简体   繁体   中英

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.

@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#

            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:

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:

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. 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:

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

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. 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. So make sure to add this too, in my answer it's this row: sqlcmd.Parameters["@UserID"].Value = userID;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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