存储过程DateTime'在集合中找不到参数'错误

时间:2019-05-21 23:13:51

标签: c# mysql stored-procedures

我有一个c#.Net程序,该程序利用MySQL.Data库连接到AWS托管的MySQL数据库。我的数据库中有一个存储过程称为“ get_dates”,其定义如下:

CREATE DEFINER=`user_name`@`%` PROCEDURE `get_dates`(IN _identifier VARCHAR(20), OUT activation_date DATETIME, OUT deactivation_date DATETIME)
BEGIN 
SELECT ifnull(ACTIVATION_DATE, "2000-01-01 12:00:00") INTO activation_date FROM table_name WHERE ID = _identifier;
SELECT ifnull(DEACTIVATION_DATE, "2000-01-01 12:00:00") INTO deactivation_date FROM table_name WHERE ID = _identifier;
END

我从程序中以这种方式调用存储过程:

MySql.Data.MySqlClient.MySqlCommand getDatesCommand = new MySql.Data.MySqlClient.MySqlCommand();
getDatesCommand.Connection = connection.connection.Connection;
getDatesCommand.CommandType = CommandType.StoredProcedure;
getDatesCommand.CommandText = "get_dates";
getDatesCommand.Parameters.Add("@_identifier", MySql.Data.MySqlClient.MySqlDbType.VarChar);
getDatesCommand.Parameters["@_identifier"].Value = identifierVar;
getDatesCommand.Parameters.Add("@activation_date", MySql.Data.MySqlClient.MySqlDbType.DateTime);
getDatesCommand.Parameters["@activation_date"].Direction = ParameterDirection.Output;
getDatesCommand.Parameters.Add("@deactivation_date", MySql.Data.MySqlClient.MySqlDbType.DateTime);
getDatesCommand.Parameters["@deactivation_date"].Direction = ParameterDirection.Output;
getDatesCommand.ExecuteNonQuery();

DateTime activation_date = (DateTime)getDatesCommand.Parameters[@"activation_date"].Value;
DateTime deactivation_date = (DateTime)getDatesCommand.Parameters[@"deactivation_date"].Value;
getDatesCommand.Dispose();

我收到System.ArgumentException:'在集合中找不到参数'activation_date'。

DateTime activation_date = (DateTime)getDatesCommand.Parameters[@"activation_date"].Value;

老实说,我不知道。任何帮助,将不胜感激。我也想继续使用DateTime变量,而不想重新格式化我的表以使用Varchars。

1 个答案:

答案 0 :(得分:1)

@Crowcoder帮助我弄清楚了。我只是想念在双引号前面而不是在双引号前面键入@的情况。感谢您的帮助