2011-03-02 76 views
1

我有一个存储过程,基本上看看是否存在一个交叉引用表中的ID ...如果存在,我想把它取回来。如果不是这样,我想创建一个新的,拿回来....这里是存储过程:MySQL的存储过程,c#和inout参数

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,才设置我outparam罚款。

当我把它在C#中我得到一个错误:你有一个无效的列序号...这里是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 

有什么建议?

在此先感谢

+0

你可以发布你的SP包括头的定义吗? – vissi 2011-03-02 00:56:27

+0

很旧但可能是(?)http://support.microsoft.com/kb/308614 – Shoban 2011-03-02 00:57:08

回答

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();,然后在关闭后尝试阅读它。