我的存储过程在代码隐藏中执行时始终返回零

本文关键字:返回 执行 存储过程 代码 隐藏 我的 | 更新日期: 2023-09-27 17:56:16

>我创建了一个用于登录的存储过程,该过程对三个表中的行进行计数以匹配电子邮件和密码,并且执行正常,除非在输出后面的代码中调用时始终为零,这是存储过程:

ALTER PROCEDURE dbo.StoredProcedure1
    (
    @Email nvarchar(50),
    @pass nvarchar(50),
    @outres int = null OUTPUT,
    @outres1 int = null OUTPUT,
    @outres2 int = null OUTPUT,
    @outres3 int = null OUTPUT
    )
AS
     DECLARE @UserId INT

    set @outres1 =  (SELECT count(*) FROM [dbo].Admins 
WHERE admin_Email=@Email AND admin_Pass=@pass)
if (@outres1=1)
 begin
 set @outres=@outres1
 SELECT @UserId = adminID
      FROM [dbo].Admins WHERE admin_Email=@Email AND admin_Pass=@pass
      set @outres1= @UserId
    RETURN @outres 
    end
set @outres2= (SELECT count(*) FROM [dbo].Instructors 
WHERE inst_Email=@Email AND inst_Pass=@pass)
if (@outres2=1)
 begin
 set @outres=@outres2
 SELECT @UserId = instructorID
      FROM [dbo].Instructors 
WHERE inst_Email=@Email AND inst_Pass=@pass
  set @outres2= @UserId
 RETURN @outres 
    end
set @outres3= (SELECT count(*) FROM [dbo].Students 
WHERE stu_Email=@Email AND stu_Pass=@pass)
if (@outres3=1)
begin
set @outres= @outres3
SELECT @UserId = studentID
      FROM [dbo].Students 
WHERE stu_Email=@Email AND stu_Pass=@pass
 set @outres3= @UserId
RETURN @outres 
end 
else
begin
set @outres=@outres3
set @outres1=0
set @outres2=0
set @outres3=0
RETURN @outres
end

如果找到,它还保存用户的用户ID。当我尝试在浏览器中登录时,我不能..这是背后的代码:

public int Validate_Login(String _Email, String _Password)
    {
        string lbl;
        string connstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(connstr);
        SqlCommand cmdselect = new SqlCommand();
        cmdselect.CommandType = CommandType.StoredProcedure;
        cmdselect.CommandText = "[dbo].[StoredProcedure1]";
        cmdselect.Parameters.Add("@Email", SqlDbType.NVarChar, 50).Value = _Email;
        cmdselect.Parameters.Add("@Password", SqlDbType.NVarChar, 50).Value = _Password;
        cmdselect.Parameters.Add("@OutRes", SqlDbType.Int, 4);
        cmdselect.Parameters.Add("@OutRes1", SqlDbType.Int, 4);
        cmdselect.Parameters.Add("@OutRes2", SqlDbType.Int, 4);
        cmdselect.Parameters.Add("@OutRes3", SqlDbType.Int, 4);
        cmdselect.Parameters["@OutRes"].Direction = ParameterDirection.Output;
        cmdselect.Parameters["@OutRes1"].Direction = ParameterDirection.Output;
        cmdselect.Parameters["@OutRes2"].Direction = ParameterDirection.Output;
        cmdselect.Parameters["@OutRes3"].Direction = ParameterDirection.Output;
        cmdselect.Connection = con;
        int Results = 0;
        try
        {
            con.Open();
            // SqlDataReader rd = cmdselect.ExecuteReader();
            cmdselect.UpdatedRowSource = UpdateRowSource.OutputParameters;
            cmdselect.ExecuteNonQuery();
            //cmdselect.ExecuteScalar();
            /* if (rd.HasRows)
              {
                  rd.Read();
                  FormsAuthentication.RedirectFromLoginPage(ETB.Text, true);
                  Response.Redirect("Welcome.aspx");
              }
              else
              {
                  lblmsg.Text = "Invalid username or password.";
              }*/
            Results = (int)cmdselect.Parameters["@OutRes"].Value;
            int res1 = (int)cmdselect.Parameters["@OutRes1"].Value;
            int res2 = (int)cmdselect.Parameters["@OutRes2"].Value;
            int res3 = (int)cmdselect.Parameters["@OutRes3"].Value;
            if (Results == 0)
            {
                Session["userID"] = null;
            }
            else
            {
                if (res1 != 0)
                {
                    Session["userID"] = res1.ToString();
                }
                else if (res2 != 0)
                {
                    Session["userID"] = res2.ToString();
                }
                else
                {
                    Session["userID"] = res3.ToString();
                }
            }


        }
        catch (SqlException ex)
        {
            lbl = ex.Message;
        }
        finally
        {
            cmdselect.Dispose();
            if (con != null)
            {
                con.Close();
            }
        }
        return Results;

    }

我已经尝试了代码中的每个注释,并且此函数始终返回零.。请帮忙...

我的存储过程在代码隐藏中执行时始终返回零

您希望在

存储过程中@pass,但传递@Password。作为旁注,请注意您的字符大小写,根据区分大小写的字符排序规则,您可能会遇到问题。

正确的代码应该是这样的;

...
cmdselect.CommandText = "[dbo].[StoredProcedure1]";
cmdselect.Parameters.Add("@Email", SqlDbType.NVarChar, 50).Value = _Email;
cmdselect.Parameters.Add("@pass", SqlDbType.NVarChar, 50).Value = _Password;
...