2011-03-02 79 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();,然後在關閉後嘗試閱讀它。