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

从数据库里老师读不到数据
if (this.IsValid)
  {
  string pwd= this.txtPassword.Text.ToString();
  //Commons.DbTools DbTools = new Commons.DbTools();
  string Pwd = Commons.DbTools.Md5(pwd); //调用DbTool的Md5方法来对密码加密
  //pwd = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "MD5"); //此种方法与上面的效果一样
  string sql = "select * from users where UserName='" + this.txtUserName.Text.ToString() + "' and Password='" + Pwd + "'";
  Common common = new Common();
  DataSet ds= common.GetDataSet(sql);

  if (ds.Tables[0].Rows.Count > 0)
  {
  this.Response.Redirect("main.aspx");
  }
  else
  {
  DbTools.MsgBox("用户名或密码错误!");
  }
  }

/////////////////////////////////////////////////////
就是这段验证用户登录的代码,老是从数据库都不到数据,所以登录时总是("用户名或密码错误!"不知道咋回事,急死了,
配置文件没有写错,因为可以从数据库导出数据来。不知道什么原因

------解决方案--------------------
if (this.IsValid)
{
string pwd= this.txtPassword.Text.ToString();
//Commons.DbTools DbTools = new Commons.DbTools();
string Pwd = Commons.DbTools.Md5(pwd); //调用DbTool的Md5方法来对密码加密
//pwd = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "MD5"); //此种方法与上面的效果一样
string sql = "select * from users where UserName='" + this.txtUserName.Text.ToString() + "' and Password='" + Pwd + "'";
 


第一你的txt 文本框本来就是string类型的,就不用 Tostring();
第二 ,密码:你应该去数据库里面看看密码是否是加过密的,你也可以直接把Pwd 换成 pwd试试,确认你的密码是否是加过密的。