c#mysql-connector varchar输出参数

时间:2015-11-06 19:22:28

标签: c# mysql mysql-connector out-parameters

我有一个mysql(在Windows 8.1上运行v5.6.25)存储过程执行以下操作:

DELIMITER $$
CREATE PROCEDURE `usp_GetData`(IN param1 int, IN param2 datetime, IN param3 datetime, OUT outparam varchar(2054))
BEGIN
    SELECT things FROM stuff WHERE a = param1 AND b = param2 AND c = param3;
    SET outparam = (SELECT GROUP_CONCAT(otherthings) FROM stuff WHERE a = param1 AND b = param2 AND c = param3);

END;

我通过mysqlconnector使用以下命令从c#调用它:

MySql.Data.MySqlClient.MySqlCommand command = new MySqlCommand();
command.CommandText = StoredProcName;
command.CommandType = System.Data.CommandType.StoredProcedure;
MySql.Data.MySqlClient.MySqlParameter outparamobj = new MySqlParameter("outparam",MySqlDbType.VarChar);
outparamobj.Direction = System.Data.ParameterDirection.Output;

command.Parameters.Add(outparamobj);
command.Parameters.AddWithValue("param1", 1);   
command.Parameters.AddWithValue("param2", startdate);
command.Parameters.AddWithValue("param3", enddate);

command.Connection = DBConn; //object set elsewhere but is an open connection.
using (MySql.Data.MySqlClient.MySqlDataReader reader = command.ExecuteReader())
{
    while (reader.Read())
    {
        //populate data object  
    }
    //to make out parameter work - ensure the reader is closed.
    if(!reader.IsClosed) reader.Close();
    string outparamvalue = command.Parameters["outparam"].Value.ToString();
}

参数的值在mysqlparameter对象中仍然是dbnull,即使它在MySQL Workbench中进行调用时返回一个值。如果我将out参数类型更改为int并返回一个项目计数,这将成功返回,因此我怀疑我没有正确定义参数的类型,但是没有成功地进行了调整。

非常感谢任何帮助!

0 个答案:

没有答案