【发布时间】:2023-03-28 15:54:01
【问题描述】:
我有一个 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: 'Parameter 'activation_date' not found in the collection。'行异常
DateTime activation_date = (DateTime)getDatesCommand.Parameters[@"activation_date"].Value;
我真的不知道。任何帮助,将不胜感激。我还想继续使用 DateTime 变量,而不是重新格式化我的表以使用 Varchars。
【问题讨论】:
-
我不使用 MySql,所以这只是一个猜测,但是您已经为您的参数分配了
@符号,但是您在没有它的情况下将它们从命令中拉回。如果你...Parameters["@activation_date"].Value;会发生什么? -
@Crowcoder 如果您发布答案,我可以将其标记为正确,以表扬您。谢谢。
标签: c# mysql stored-procedures