存儲過程我有一個存儲過程是這樣的:有返回值
ALTER PROCEDURE [dbo].[AuthenticateUser]
@AzUserName varchar(20),
@Hash varchar(32),
@UserId bigint output,
@Authorized bit output
...
,並在Management Studio中運行良好罰款。 這裏是我的C#代碼:
SqlConnection scon = new SqlConnection(connectionString);
SqlCommand authCmd = new SqlCommand("AuthenticateUser", scon);
authCmd.CommandType = System.Data.CommandType.StoredProcedure;
SqlParameter userNameParam = authCmd.Parameters.Add("@AzUserName", System.Data.SqlDbType.VarChar, 20);
userNameParam.Value = username;
string hashed = Md5Hash.ComputeHash(username);
SqlParameter hashedParam = authCmd.Parameters.Add("@Hash", System.Data.SqlDbType.VarChar, 32);
hashedParam.Value = hashed;
SqlParameter userIdParam = authCmd.Parameters.Add("@UserId", System.Data.SqlDbType.Int);
userIdParam.Direction = System.Data.ParameterDirection.ReturnValue;
SqlParameter authorizedParam = authCmd.Parameters.Add("@Authorized", System.Data.SqlDbType.Bit);
authorizedParam.Direction = System.Data.ParameterDirection.ReturnValue;
scon.Open();
authCmd.ExecuteNonQuery();
scon.Close();
當我運行它,我收到以下錯誤:
{"Procedure or function 'AuthenticateUser' expects parameter '@UserId', which was not supplied."} System.Exception {System.Data.SqlClient.SqlException}
當我與ParameterDirection.Output我沒有收到錯誤,但從來沒有得到更換ParameterDirection.ReturnValue程序的價值。
更新: 謝謝大家的幫助。錯誤比你想象和我在問題中描述的更加微不足道。今天我一直在不斷地改變ReturnValue到Output,沒有結果。然後,我不得不在SO上發佈我的問題,只是意識到我正在使用...用戶名的散列值。現在去戶外獲取一些氧氣。
請注意存儲過程參數列表** @ UserId bigint輸出,** – Kaf 2012-03-12 16:23:27
您的參數是'OUTPUT' - 所以你需要將它們定義爲'ParameterDirection.Output' - ** not **'.ReturnValue' ... – 2012-03-12 16:29:33