日期:2014-05-18  浏览次数:20400 次

asp.net c#设计登陆代码,总是提示输入错误,但是登录名密码是正确的
SqlConnection CN = new SqlConnection("server=IT-PC;uid=sa;pwd=123456;database=DiscreteMath1");  //初始化连接字符串    
  SqlCommand sqlCmd = new SqlCommand("UserLogin", CN);
  sqlCmd.Connection = CN;
  CN.Open();//添加命令,从数据库中得到数据
  string UserLogin = "select * from User_info where UserID= '" + UserID.Text.Trim() + "'and Pwd = '" + Pwd.Text.Trim() + "'";
  if (UserID.Text.ToString() == "UserID" && Pwd.Text.ToString() == "Pwd")
  {
  Response.Redirect("HomePage.aspx", true);
  }
  else
  {
  Msg.Text = "登录名或密码错误!";
  }

------解决方案--------------------
你这个根本都没从数据库中查出来,你这个只是文本框输入的值和你自己给的UserID和Pwd
比较。如果文本框值没输入错误的话,他会一直执行Response.Redirect("HomePage.aspx", true);
你要实现登录
 string UserLogin = "select count(1) from User_info where UserID= '" + UserID.Text.Trim() + "'and Pwd = '" + Pwd.Text.Trim() + "'";
int i=int.Parse(cmd.ExcuteScalar());//手写的,自己改改
if(i>0)
{
Response.Redirect("HomePage.aspx", true);
}
else
{
Msg.Text = "登录名或密码错误!";
}
或者是用SqlDataReader读出来UserID和Pwd和输入的文本框值进行比较判断

------解决方案--------------------
SqlConnection CN = new SqlConnection("server=IT-PC;uid=sa;pwd=123456;database=DiscreteMath1"); //初始化连接字符串
SqlCommand sqlCmd = new SqlCommand("UserLogin", CN);
sqlCmd.Connection = CN;
CN.Open();//添加命令,从数据库中得到数据
string UserLogin = "select * from User_info where UserID= '" + UserID.Text.Trim() + "' and Pwd = '" + Pwd.Text.Trim() + "'";
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
if (UserID.Text.ToString().Trim() == dr["UserID"])
{
if( Pwd.Text.ToString().Trim()==dr["Pwd"])
{
Response.Redirect("HomePage.aspx");
}
else
{
Msg.Text = "密码错误!";
}
}
else
{
Msg.Text = "登录名错误!";
}
}
------解决方案--------------------
可以这样子
 SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["数据库名"].ConnectionString);
conn.Open();
string sqlstr = "select * from User_info where UserID='" + UserID.Text + "' and Pwd ='" + Pwd.Text + "'";
SqlCommand comm = new SqlCommand(sqlstr, conn);
SqlDataReader dr = comm.ExecuteReader();
if (dr.Read())
{
Response.Redirect("HomePage.aspx");
}
else
{
Response.Write("<script language='javascript'>alert('用户名或密码错误!')</script>");
}
conn.Close();
------解决方案--------------------
探讨

这个方法好,但是总是提示找不到userlogin的存储过程,引用:
SqlConnection CN = new SqlConnection("server=IT-PC;uid=sa;pwd=123456;database=DiscreteMath1"); //初始化连接字符串
SqlCommand sqlCmd = new SqlCommand("UserLogin", ……

------解决方案--------------------
SqlConnection CN = new SqlConnection("server=IT-PC;uid=sa;pwd=123456;database=DiscreteMath1"); //初始化连接字符串