简体   繁体   English

如何从 SQL 存储过程返回输出 varchar 值

[英]How to return output varchar value from SQL stored procedure

I am trying to get a varchar value from SQL to use in function in C# here is how i tried to create Stored Procedure to return ,i appreciate any help.我正在尝试从 SQL 获取 varchar 值以在 C# 中的函数中使用这里是我如何尝试创建存储过程以返回,我感谢任何帮助。

@result @结果

as an output value作为输出值

CREATE PROCEDURE [dbo].[RolValidation]
@UserID int,
@result varchar(50) output

AS
BEGIN
Select @result=role from LogUser where  UserID=@UserID


end

And here is the function in C#这是 C# 中的函数

            if (sqlCon.State == ConnectionState.Closed)
                sqlCon.Open();
            SqlCommand sqlcmd = new SqlCommand("RolValidation", sqlCon);
            sqlcmd.CommandType = CommandType.StoredProcedure;

            sqlcmd.Parameters.AddWithValue("@UrunID", SqlDbType.Int);
            sqlcmd.Parameters.Add("@result", SqlDbType.NVarChar,50).Direction = ParameterDirection.Output;
            string rol = Convert.ToString(sqlcmd.Parameters["@UrunID"].Value);
            sqlcmd.ExecuteNonQuery();
            returnValue = rol;

It looks like that the code from your question is not the real code that you are working on so there might be other reasons why you don't get your value back.看起来您问题中的代码并不是您正在处理的真实代码,因此可能有其他原因导致您无法收回价值。

However, judging from what I see if you modify the SP a little:但是,从我看到的情况来看,如果您稍微修改一下 SP:

CREATE PROCEDURE [dbo].[RolValidation]
      @UserID INT;
AS
BEGIN
      SET NOCOUNT ON;

      DECLARE @result varchar(50);

      SET @result = Select role from LogUser where  UserID=@UserID;

      RETURN @result;
END

And in your C# code:在你的 C# 代码中:

if (sqlCon.State == ConnectionState.Closed)
{
    sqlCon.Open();
}

SqlCommand sqlcmd = new SqlCommand("RolValidation", sqlCon);
sqlcmd.CommandType = CommandType.StoredProcedure;

sqlcmd.Parameters.AddWithValue("@UserID", SqlDbType.Int);
sqlcmd.Parameters["@UserID"].Value = userID;

var rol = cmd.Parameters.Add("@result", SqlDbType.NVarChar, 50);
rol.Direction = ParameterDirection.ReturnValue


sqlcmd.ExecuteNonQuery();
returnValue = rol.Value;

Basically there were two main issues with your original code 1 - you should SET the value and then "RETURN" it.基本上,您的原始代码 1 存在两个主要问题 - 您应该SET该值,然后“返回”它。 You can set it inline the SELECT query as you have done, but I kinda like this explicit approach a bit more 2 - you were trying to get the value before executing the query:您可以将其设置为内联SELECT查询,但我更喜欢这种显式方法 2 - 您试图在执行查询之前获取值:

string rol = Convert.ToString(sqlcmd.Parameters["@UrunID"].Value);
sqlcmd.ExecuteNonQuery();

which is wrong, it should be the other way around - first execute the query then try to get the .Value这是错误的,应该是相反的 - 首先执行查询然后尝试获取.Value

Other than that I'm not aware of the entire code but it's a good practice to wrap the connection and the command in using statements.除此之外,我不知道整个代码,但在using语句中包装连接和命令是一个很好的做法。 If you don't have a good reason not to do that I suggest to change it.如果你没有充分的理由不这样做,我建议你改变它。

PS Now I see that you are not passing the UserID value, at least in the code from the original question. PS 现在我看到您没有传递UserID值,至少在原始问题的代码中是这样。 So make sure to add this too, in my answer it's this row: sqlcmd.Parameters["@UserID"].Value = userID;所以一定要添加这个,在我的回答中是这一行: sqlcmd.Parameters["@UserID"].Value = userID;

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

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