简体   繁体   English

带有返回值的存储过程

[英]Stored procedure with return value

I have a stored procedure that goes like this: 我有一个存储过程,如下所示:

ALTER PROCEDURE [dbo].[AuthenticateUser]    
    @AzUserName varchar(20),
    @Hash varchar(32),
    @UserId bigint output,
    @Authorized bit output
    ...

and runs just fine fine in Management Studio. 并且在Management Studio中运行得很好。 Here's my C# code: 这是我的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();

When I run it I am getting the following error: 当我运行它时,我收到以下错误:

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

When I replace ParameterDirection.ReturnValue with ParameterDirection.Output I am not getting the error but never get the value of the procedure. 当我用ParameterDirection.Output替换ParameterDirection.ReturnValue时,我没有收到错误,但从未获得过程的值。

UPDATE: Thank you All for your help. 更新:谢谢大家的帮助。 The error was more trivial than you would have thought and I described in the question. 这个错误比你想象的更微不足道,我在问题中描述过。 I have been changing back and forth ReturnValue to Output for quite a while today with no result. 今天我一直在来回变换ReturnValue到输出很长一段时间没有结果。 Then I had to post my question on SO just to realize that I am taking the hash value of ... username..Going outdoor to get some oxygen now. 然后我不得不在SO上发布我的问题,只是为了意识到我正在使用哈希值...用户名。现在去室外得到一些氧气。

You will have to use ParameterDirection.Output on every parameter, that has been marked with output in T-SQL. 您必须对每个参数使用ParameterDirection.Output ,该参数已在T-SQL中标记为output You can access the values, after the call to 您可以在致电后访问这些值

authCmd.ExecuteNonQuery();

by getting the values of the parametes like this: 通过获取参数的值,如下所示:

authCmd.Parametes["@UserId"].Value

You're confusing the concepts of OUTPUT and RETURN values. 你混淆了OUTPUT和RETURN值的概念。

A RETURN value from a stored procedure is a single integer value per stored procedure that is defined within your proc by using the RETURN statement eg 来自存储过程的RETURN值是每个存储过程的单个整数值,它通过使用RETURN语句在proc中定义,例如

RETURN 1  

A stored procedure can have zero to many parameters of which zero to many can be defined as OUTPUT. 存储过程可以具有零到多个参数,其中零到多个可以定义为OUTPUT。

In your case you're not showing any use of the RETURN statement but you are using OUTPUT parameters. 在您的情况下,您没有显示任何RETURN语句的使用,但您正在使用OUTPUT参数。 In SQL Server these are more like input/output parameters and you need to provide a value. 在SQL Server中,这些更像是输入/输出参数,您需要提供一个值。

You can access the resulting value of an OUTPUT parameter by looking at the parameters collection after calling the stored procedure and look at the value eg 通过在调用存储过程后查看参数集合并查看值,可以访问OUTPUT参数的结果值

authCmd.Parameters[2].Value

Or 要么

userIdParam.Value

As per other answers, you need to use the output parameter direction to achieve this 根据其他答案,您需要使用输出参数方向来实现此目的

You can access the values of authorizedParam.Value and userIdParam.Value after executing the command. 执行命令后,可以访问authorizedParam.Value和userIdParam.Value的值。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM