如何在代码中使用存储过程的返回值来验证

时间:2014-06-22 19:48:41

标签: c# sql sql-server

这是我的SQL Server存储过程,用于验证用户凭据。

ALTER procedure [dbo].[ValidateUserCredentials]
   @Username varchar(50),
   @Password varchar(50),
   @result varchar(50) out
AS
BEGIN
   IF EXISTS(select Username, Password 
             from Users
             where Username = @Username
               AND Password = @Password)
   BEGIN
      set @result = 1
   END
   ELSE
   BEGIN
      set @result = 0
   END

END

这是我在重用类中的方法。

public bool executeInsertprocedure(string spName, SqlParameter[] sqlParameters, out string message)
{
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = conn;
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = spName;
    cmd.Parameters.AddRange(sqlParameters);

    conn.Open();

    try
    {
       cmd.ExecuteNonQuery();
       message = sqlParameters[sqlParameters.Length - 1].Value.ToString();
       return true;
    }
    catch
    {
       message = "Something is wrong!";
       return false;
    }
}

我在这里用BLL调用它。

public void verifyUserCredentials(string userName, string password,string message,out string sp_message)
{
   SqlParameter[] param = new SqlParameter[3];
   param[0] = new SqlParameter("@UserName", SqlDbType.VarChar);
   param[0].Value = userName;

   param[1] = new SqlParameter("@Password", SqlDbType.VarChar);
   param[1].Value = password;

   param[2] = new SqlParameter("@result", SqlDbType.VarChar);
   param[2].Direction = ParameterDirection.Output;
   //param[2].Value = sp_message;

   objRuse = new Reuse();
   objRuse.executeInsertprocedure("ValidateUserCredentials", param, ??);
}

这是我的登录页面。

protected void btnSignin_Click(object sender, EventArgs e)
{
       objBll = new bll();
       objBll.verifyUserCredentials(txtSignInUsername.Text,txtSignInPassword.Text, ??);

       if(?? == true)
       {
            Session["Username"]=txtSignInUsername;
            Response.Redirect("Default.aspx");
        }
        else
        {
            Response.Redirect("Login.aspx");
        }

    }

我应该放什么?在我的代码中的地方。如何获取存储过程的返回值。

1 个答案:

答案 0 :(得分:2)

将您的程序更改为@result int out@result bit out而不是varchar(50)

更改方法executeInsertprocedure

public bool executeInsertprocedure(string spName, SqlParameter[] sqlParameters, out int message)
{ 
   try
    {
       cmd.ExecuteNonQuery();
       message = cmd.Parameters["@result"].Value;       
       (message == 1) ? return true : return false;
    }
}

您的verifyUserCredentials方法

public void verifyUserCredentials(string userName, string password,string message,out int sp_message)
{
   objRuse = new Reuse();
   objRuse.executeInsertprocedure("ValidateUserCredentials", param, sp_message);
}

您的btnSignin_Click活动

protected void btnSignin_Click(object sender, EventArgs e)
{
 int errorid;
 objBll = new bll();
 if(objBll.verifyUserCredentials(txtSignInUsername.Text,txtSignInPassword.Text, out errorid))
       {          
            Session["Username"]=txtSignInUsername;
            Response.Redirect("Default.aspx");
        }
       else 
         {
           //Else part code
         }
}