2012-03-12 78 views
2

存儲過程我有一個存儲過程是這樣的:有返回值

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上發佈我的問題,只是意識到我正在使用...用戶名的散列值。現在去戶外獲取一些氧氣。

+1

請注意存儲過程參數列表** @ UserId bigint輸出,** – Kaf 2012-03-12 16:23:27

+0

您的參數是'OUTPUT' - 所以你需要將它們定義爲'ParameterDirection.Output' - ** not **'.ReturnValue' ... – 2012-03-12 16:29:33

回答

4

對於已在T-SQL中標記爲output的每個參數,您將不得不使用ParameterDirection.Output

authCmd.Parametes["@UserId"].Value 
+0

準確地說 - 更清楚的是:OP需要使用ParameterDirection.Output而不是ParameterDirection .ReturnValue' ... – 2012-03-12 16:30:28

1

您可以執行後訪問authorizedParam.Value和userIdParam.Value的價值觀:你可以通過獲取指標的影響的值,這樣的訪問值,通話後

authCmd.ExecuteNonQuery(); 

命令。

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 = Zonal.Pie.Core.Common.Utils.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.Output; 

SqlParameter authorizedParam = authCmd.Parameters.Add("@Authorized", System.Data.SqlDbType.Bit); 
authorizedParam.Direction = System.Data.ParameterDirection.Output; 

scon.Open(); 
authCmd.ExecuteNonQuery(); 
//Access authorizedParam.Value and userIdParam.Value here 
scon.Close(); 
2

令人困惑的是OUTPUT和RETURN值的概念。

從存儲過程返回值是在每個存儲過程的單個整數值是通過使用RETURN語句例如你的PROC內限定

RETURN 1 

存儲過程可以有0到多個參數,這些參數的零很多可以定義爲OUTPUT。

在你的情況下,你沒有顯示任何使用RETURN語句,但你正在使用OUTPUT參數。在SQL Server中,這些更像輸入/輸出參數,您需要提供一個值。

您可以通過查看參數集調用存儲過程後訪問產生的輸出參數值,並查看值,例如,

authCmd.Parameters[2].Value 

或者

userIdParam.Value 

按其他的答案,您需要使用輸出參數方向來實現此目的