2012-03-12 11 views
2

を持つストアドプロシージャを。ここ は私の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(); 

私は、次のエラーを取得しています:私はParameterDirection.OutputでParameterDirection.ReturnValueを交換した場合

{"Procedure or function 'AuthenticateUser' expects parameter '@UserId', which was not supplied."} System.Exception {System.Data.SqlClient.SqlException} 

私はエラーを取得していないが、取得することはありませんしていますプロシージャの値

更新日: ありがとうございました。このエラーはあなたが思っていたよりも些細なことでした。私は結果を返すために、今日、かなり長い間、ReturnValueをOutputに前後に変更してきました。それから、私はあなたのハッシュ値を取っていることに気付くだけで、私の質問を投稿しなければなりませんでした。ユーザーネーム。

+1

ストアドプロシージャのパラメータリストに注意してください** @ UserId bigint出力、** – Kaf

+0

あなたのパラメータ'OUTPUT'ですので、' ParameterDirection.Output'として定義する必要があります - ** not ** '.ReturnValue' ... –

答えて

4

T-SQLでoutputとマークされているすべてのパラメータにParameterDirection.Outputを使用する必要があります。

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

正確に - OPは' ParameterDirection.Output'を 'ParameterDirection'の代わりに使う必要があります.ReturnValue' ... –

1

はあなたが実行した後authorizedParam.ValueとuserIdParam.Valueの値にアクセスすることができます:あなたはこのようparametesの値を取得することによって

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 1 

ストアドプロシージャを使用して、procの内で定義されたストアドプロシージャごとに単一の整数値でありますOUTPUTと定義することができます。

あなたの場合は、RETURNステートメントの使用は表示されていませんが、OUTPUTパラメーターを使用しています。 SQL Serverでは、これらは入力/出力パラメータによく似ており、値を提供する必要があります。

は、ストアドプロシージャを呼び出した後のパラメータのコレクションを見て、OUTPUTパラメータの結果の値にアクセスし、値など他の回答を1として

authCmd.Parameters[2].Value 

それとも

userIdParam.Value 

を見ることができます、あなたはこれを達成するために出力パラメータの方向を使用する必要があります

関連する問題