我在MySQL数据库上创建了一个存储过程,如果我直接从Navicat(一个SGBD)运行它,它可以工作正常但如果我尝试从C#代码运行存储过程的脚本,我会收到一个错误"参数' @ req'必须定义"。我的代码如下:
DROP PROCEDURE IF EXISTS SP_GET_EXPERIENCE_RECORD_STATISTICS;
CREATE PROCEDURE SP_GET_EXPERIENCE_RECORD_STATISTICS(IN startTime VARCHAR(19), IN endTime VARCHAR(19), IN isEquipment INT)
BEGIN
DECLARE req VARCHAR(1000);
SET @req = "SELECT ";
IF(isEquipment = 1) THEN
SET @req = CONCAT(@req, "ClientName,");
ELSE
SET @req = CONCAT(@req, "ContentName 'ClientName',");
END IF;
SET req = CONCAT(req, " COUNT(1) 'ConsumeCount',
SUM(Points) 'PointsCount',
SUM(CASE WHEN Mode = 1 THEN 1 ELSE 0 END) 'CardCount',
SUM(CASE WHEN Mode = 1 THEN Points ELSE 0 END) 'CardPoints',
SUM(CASE WHEN Mode = 0 THEN 1 ELSE 0 END) 'WeChatCount',
SUM(CASE WHEN Mode = 0 THEN Points ELSE 0 END) 'WeChatPoints'
FROM experiencerecord
WHERE EndTime BETWEEN '", startTime, "' AND '", endTime, "'");
IF (isEquipment = 1) THEN
SET @req = CONCAT(@req, " GROUP BY ClientName");
ELSE
SET req = CONCAT(req, " GROUP BY ContentName");
END IF;
PREPARE stmt FROM @req;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END;
-- CALL SP_GET_EXPERIENCE_RECORD_STATISTICS('2010-01-01 00:00:00', '2016-12-31 23:59:59', 0);
-- CALL SP_GET_EXPERIENCE_RECORD_STATISTICS('2010-01-01 00:00:00', '2016-12-31 23:59:59', 1);
我在C#中的代码:
MySqlConnection conn = new MySqlConnection(Properties.Settings.Default.DatabaseConnectionString);
public bool mysql(string sql)
{
try
{
MySqlCommand comm = new MySqlCommand(sql, conn);
MySqlDataAdapter mda = new MySqlDataAdapter(comm);
DataSet ds = new DataSet();
mda.Fill(ds);
return true;
}
catch (Exception ex)
{
return false;
throw ex;
}
}
任何想法来自哪里都将非常感激。
答案 0 :(得分:3)
最后我添加了Allow User Variables = True;到connectionString,现在它工作得很好。
答案 1 :(得分:0)
似乎是拼写错误
[ec2-user ~]$ sudo groupadd www
[ec2-user ~]$ sudo usermod -a -G www ec2-user
[ec2-user ~]$ sudo chown -R root:www /var/www
[ec2-user ~]$ sudo chmod 2775 /var/www
[ec2-user ~]$ find /var/www -type d -exec sudo chmod 2775 {} \;
[ec2-user ~]$ find /var/www -type f -exec sudo chmod 0664 {} \;
此外,由于# phpMyAdmin - Web based MySQL browser written in php
#
# Allows only localhost by default
#
# But allowing phpMyAdmin to anyone other than localhost should be considered
# dangerous unless properly secured by SSL
Alias /phpMyAdmin /usr/share/phpMyAdmin
Alias /phpmyadmin /usr/share/phpMyAdmin
<Directory /usr/share/phpMyAdmin/>
AddDefaultCharset UTF-8
<IfModule mod_authz_core.c>
# Apache 2.4
<RequireAny>
Require ip my_ip
Require ip ::1
</RequireAny>
</IfModule>
<IfModule !mod_authz_core.c>
# Apache 2.2
Order Deny,Allow
Deny from All
Allow from my_ip
Allow from ::1
</IfModule>
</Directory>
<Directory /usr/share/phpMyAdmin/setup/>
<IfModule mod_authz_core.c>
# Apache 2.4
<RequireAny>
Require ip my_ip
Require ip ::1
</RequireAny>
</IfModule>
<IfModule !mod_authz_core.c>
# Apache 2.2
Order Deny,Allow
Deny from All
Allow from my_ip
Allow from ::1
</IfModule>
</Directory>
# These directories do not require access over HTTP - taken from the original
# phpMyAdmin upstream tarball
#
<Directory /usr/share/phpMyAdmin/libraries/>
Order Deny,Allow
Deny from All
Allow from None
</Directory>
<Directory /usr/share/phpMyAdmin/setup/lib/>
Order Deny,Allow
Deny from All
Allow from None
</Directory>
<Directory /usr/share/phpMyAdmin/setup/frames/>
Order Deny,Allow
Deny from All
Allow from None
</Directory>
# This configuration prevents mod_security at phpMyAdmin directories from
# filtering SQL etc. This may break your mod_security implementation.
#
#<IfModule mod_security.c>
# <Directory /usr/share/phpMyAdmin/>
# SecRuleInheritance Off
# </Directory>
#</IfModule>
是参数,我在C#代码中看不到任何IF (isEquipment = 1) THEN
SET @req = CONCAT(@req, " GROUP BY ClientName");
ELSE
SET @req = CONCAT(@req, " GROUP BY ContentName"); //You forgot @req here!
END IF;
行。
AddWithParameter