使用C#代码在MySQL中创建存储过程

时间:2014-11-06 11:23:35

标签: c# mysql stored-procedures

我正在尝试使用以下C#代码在MySQL中创建存储过程:

using (MySqlConnection conn = new MySqlConnection(connectionString))
        {
            conn.Open();

            using (MySqlTransaction trans = conn.BeginTransaction())
            {
                using (MySqlCommand command = conn.CreateCommand())
                {
                    command.CommandText = spscript;

                    command.ExecuteNonQuery();
                }

                trans.Commit();
            }
        }

,SP脚本如下

DROP procedure IF exists SP_GetMyBinWorkflowResult;

CREATE PROCEDURE `SP_GetResult` (p_tablename varchar(100), p_binId int, p_queueId int)
BEGIN
declare whereclause varchar(1000);
declare success  int;
declare @s varchar(4000);

set whereclause = "1=1";
/*prepare string for where clause*/
set whereclause = concat(whereclause, " and queueid=", p_queueId, " and isdeleted = 0 and isassigned = 1  and binid =", p_binId  );

SET @s = CONCAT('SELECT count(*) FROM ',p_tablename, ' where ', whereclause);
    PREPARE stmt FROM @s;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;

SET @s = CONCAT('SELECT * FROM ',p_tablename, ' where ', whereclause);

    PREPARE stmt FROM @s;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;

END

当我运行此代码时,它会给出异常,因为“参数'@s'必须定义为”。

我在脚本中声明了变量@s。但不确定为什么它必须定义“参数'@s'。”任何人都可以帮我解决这个错误。

感谢。

0 个答案:

没有答案