如何使用存储过程在asp.net中创建登录表单?

时间:2018-03-18 17:51:55

标签: asp.net sql-server-2008

我是编码和asp.net的新手,任何人都可以帮助我我尝试使用存储过程创建用户登录但我收到错误。

我发布错误的代码请检查并帮助我。感谢

数据访问代码:

public class DataAcc
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);

    public DataSet DA_Bind()
    {
        SqlCommand cmd = new SqlCommand("Sp_select", con);

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        return ds;
    }
}

业务逻辑代码:

public class business
{
    SqlCommand cmd = new SqlCommand();
    DataAcc sda = new DataAcc();

    public int id { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }

    public bool getuser()
    {
        if(sda.DA_Bind().Tables[0].Rows.Count==0)
        {
            cmd.Parameters.AddWithValue("@UseName", UserName);
            cmd.Parameters.AddWithValue("@Passwors", Password);
            return false;
        }
        else
        {
            return true;
        }
    }
}  

登录页面代码

public partial class login : System.Web.UI.Page
{
    protected void btnSave_Click(object sender, EventArgs e)
    {
        business obj = new business();
        obj.UserName = txtUser.Text;
        obj.Password = txtEmail.Text;

        if(obj.getuser())
        {
            Session["User"] = obj.UserName;
            Response.Redirect("mainpage.aspx");
        }
        else
        {
            Response.Redirect("Please enter correct username and password");
        }
    }
 }

存储过程:

Alter procedure Sp_select
    (@UserName varchar(50),
     @Password varchar(50))
AS 
BEGIN
    Select * 
    from tb_login 
    where UserName = @UserName 
      and Password = @Password
END

1 个答案:

答案 0 :(得分:0)

在执行程序之前,你应该提供@UseName和@Passwors,记住拼写错误:)。像这样:

public DataSet DA_Bind(string user, string password)
{
    SqlCommand cmd = new SqlCommand("Sp_select", con);

    cmd.CommandType = CommandType.StoredProcedure; 
    cmd.Parameters.Add("@UseName", SqlDbType.VarChar).Value = user;
    cmd.Parameters.Add("@Passwors", SqlDbType.VarChar).Value = password;

    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    return ds;
}