简体   繁体   English

无法从存储过程获取返回值

[英]Cannot get return value from stored procedure

I know this question has been asked many many times, I've gone through most of them but none resolved my issue 我知道这个问题已经被问过很多次了,我经历了大多数问题,但没有一个解决了我的问题

I have a stored procedure called LoginValidation which takes 2 parameters, username and password and returns the user_id of the corresponding user. 我有一个称为LoginValidation的存储过程,该过程使用2个参数(用户名和密码)并返回相应用户的user_id

Here is the procedure code: 这是过程代码:

    @username nvarchar(50),
    @password nvarchar(50),
    @userID int output
AS
    SELECT @userID = user_id
    FROM Users_Master 
    WHERE user_name = @username and password = @password;

    RETURN

But, when I'm trying to use this procedure in C# the following error occurs: 但是,当我尝试在C#中使用此过程时,会发生以下错误:

Procedure or function 'LoginValidation' expects parameter '@userID', which was not supplied 过程或函数“ LoginValidation”需要未提供的参数“ @userID”

Here is my C# code: 这是我的C#代码:

protected void btnLogin_Click(object sender, EventArgs e)
{
    cmd.Connection = con;

    con.Open();

    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "LoginValidation";

    cmd.Parameters.Add("@username", SqlDbType.VarChar).Value = txtmobile.Text;
    cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = txtpwd.Text;

    SqlParameter returnParm = cmd.Parameters.AddWithValue("@userID", SqlDbType.VarChar);
    returnParm.Direction = ParameterDirection.ReturnValue;

    string id = returnParm.Value.ToString();

    try
    {
        dr = cmd.ExecuteReader();
    }
    catch (SqlException EX)
    {
        string ff = EX.Message;
    }

    if (dr.HasRows)
    {
        while (dr.Read())
        {
            Session["name"] = dr["user_name"].ToString();

            lblmsg.Text = "Login successful: " + id;
        }
    }
    else
    {
        lblmsg.Text = "Invalid username/password";
    }

    dr.Close();
    con.Close();
}

Any suggestions please? 有什么建议吗?

尝试使用returnParm.Direction = ParameterDirection.Output;

Be aware that you're using an incorrect method AddWithValue while passing the data-type. 请注意,在传递数据类型时,您使用了不正确的方法AddWithValue You need to change that to: 您需要将其更改为:

SqlParameter returnParm = cmd.Parameters.Add("@userID", SqlDbType.VarChar);
returnParm.Direction = ParameterDirection.Output;

Also you need to set the Direction to Output 另外,您需要将方向设置为Output

Then you need to read the value after executing the query: 然后,您需要执行查询读取值:

dr = cmd.ExecuteReader();
string id = returnParm.Value.ToString();

Docs : 文件

Input : The parameter is an input parameter. 输入 :参数是输入参数。

InputOutput : The parameter is capable of both input and output. InputOutput :该参数可以输入和输出。

Output : The parameter is an output parameter. 输出 :该参数是输出参数。

ReturnValue : The parameter represents a return value from an operation such as a stored procedure, built-in function, or user-defined function. ReturnValue :该参数表示操作(例如存储过程,内置函数或用户定义的函数)的返回值。

First you decide that you want to get output parameter or return value parameter. 首先,您决定要获取输出参数或返回值参数。

  1. to get return value parameter. 获取返回值参数。 You don't have to pass any extra parameter to stored procedure. 您不必将任何额外的参数传递给存储过程。 just add below lines 只需添加以下行

     SqlParameter returnParameter = cmd.Parameters.Add("RetVal", SqlDbType.Int); returnParameter.Direction = ParameterDirection.ReturnValue; cmd.ExecuteNonQuery(); int id = (int) returnParameter.Value; 
  2. to get output parameter. 获取输出参数。 All the steps you done are fine. 您完成的所有步骤都很好。 just replace 只需更换

     returnParm.Direction = ParameterDirection.ReturnValue; 

with

  returnParm.Direction = ParameterDirection.Output;

you are doing mistake. 你做错了。 Call 呼叫

string id = returnParm.Value.ToString();

after

dr = cmd.ExecuteReader();

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

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