【问题标题】:MySQL Stored Procedures, c# and inout parametersMySQL 存储过程、c# 和 inout 参数
【发布时间】:2011-07-06 22:37:02
【问题描述】:

我有一个存储过程,它主要查看交叉引用表中是否存在 ID...如果存在,我想取回它。如果没有,我想创建一个新的并将其取回......这是存储的过程:

BEGIN

declare data_found int default 1;
declare l_id int default -1;

declare continue handler for 1329
  set data_found=0;

Set p_unique_id = -1;

begin

  select unique_id, is_default into p_unique_id, p_is_default  from jmax.ids where alt_number=p_alt_num;
end;

if p_unique_id>0 then
   set p_existed=1;
else
  insert into jmax.ids (alt_number,is_default) VALUES (p_alt_num,p_is_default);
  set p_existed=0;
  select unique_id into p_unique_id from jmax.ids where alt_number=p_alt_num;

end if;

END

我用它应该找到的值在 dforge 中运行它,它确实设置了我的超参数。

当我在 c# 中调用它时,我收到一个错误:You have an invalid Column Ordinal...here is the c#:

DBAccess.DBUtils dbObj =   DBAccess.DBUtils.Instance();
        MySqlDataReader theReader;
        MySqlCommand theCommand = new MySqlCommand("TestInOut", dbObj.GetLocalConnection());

        MySqlParameter p_alt_num, p_is_default, p_unique_id, p_existed;
        theCommand.CommandType = CommandType.StoredProcedure;

        p_alt_num = theCommand.Parameters.Add("p_alt_num", MySqlDbType.VarChar);
        p_alt_num.Value = "12044";  //my text value
        p_is_default = theCommand.Parameters.Add("p_is_default", MySqlDbType.Int32);
        p_unique_id = theCommand.Parameters.Add("p_unique_id", MySqlDbType.Int32);
        p_unique_id.Direction = ParameterDirection.InputOutput;
        p_existed = theCommand.Parameters.Add("p_existed", MySqlDbType.Int32);
        p_existed.Direction = ParameterDirection.InputOutput;

        theReader = theCommand.ExecuteReader();
        theReader.Close();

        Console.WriteLine("unique ID = <" + theReader.GetInt32(1));  //this line blows up

有什么建议吗?

提前致谢

【问题讨论】:

标签: c# mysql stored-procedures


【解决方案1】:

ExecuteReader 期待来自您的存储过程的表结果集,但它看起来不像(您使用SELECT ... INTO 而不是SELECT ... FROM)。尝试使用ExecuteNonQuery 并从参数本身读取值。

// retVal contains the number of records affected by the query. It may return
// the number of rows found, not sure for mysql.
int retVal = theCommand.ExecuteNonQuery();
Console.WriteLine("unique ID = <{0}", p_unique_id.Value);

另外请注意,您在关闭阅读器后无法访问它的数据。在您的示例中,您调用theReader.Close();,然后在它关闭后尝试阅读它。

【讨论】:

    猜你喜欢
    • 2012-09-12
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-10
    • 1970-01-01
    相关资源
    最近更新 更多