我有一个包含用户和密码控件的登录页面。当我单击登录按钮时,脚本使用存储过程(LoginCheck)检查数据库表(寄存器)中的用户和密码的可用性,但是当我输入数据时它将永远不会成功,它总是会失败。
以下是代码脚本:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LoginBtn_Click(object sender, EventArgs e)
{
DataTable dt= new DataTable();
SqlDataAdapter adp = new SqlDataAdapter();
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Mydatabase"].ConnectionString);
try
{
SqlCommand cmd = new SqlCommand("LoginCheck", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@User", TextUsr.Text.Trim().ToString());
cmd.Parameters.AddWithValue("@Password", TextPass.Text.Trim().ToString());
adp.SelectCommand = cmd;
adp.Fill(dt);
cmd.Dispose();
if (dt.Rows.Count > 0)
{
lblStatus.Text = "Login Successfully" + dt.Rows.Count.ToString();
clearcontrols();
}
else
{
lblStatus.Text = "Wrong username of password "+dt.Rows.Count.ToString();
}
}
catch (Exception ex)
{
lblStatus.Text = ex.Message.ToString();
}
finally
{
// reader.Close();
dt.Clear();
dt.Dispose();
adp.Dispose();
conn.Close();
}
}
private void clearcontrols()
{
TextUsr.Text = "";
TextPass.Text = "";
}
}
以下是存储过程:
CREATE PROCEDURE LoginCheck
@User nvarchar(50),
@Password nvarchar(50)
AS
BEGIN
Select * from Register where User=@User and Password=@Password
END
答案 0 :(得分:0)
我找到导致登录失败的原因,它是表寄存器中的列名用户,它与框架中的系统用户名冲突,我更改为用户名,现在工作正常。