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

这是我做的一个登陆的代码,不管用户名和密码是正确还是错误都提示登陆失败。cont为-1哪位帮忙解决一下
if (Request.HttpMethod=="POST")
{
  string name = Request.Form["textfield"].ToString();
  string pwd = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Request.Form["textfield2"].ToString(),"MD5");
  string sql = "select * from user where user_account='"+name+"' and user_pwd='"+pwd+"'";
  string str = "server=localhost;user id=root;database=tfdo;password=97381687";
  MySqlConnection mycoon = new MySqlConnection(str);
  MySqlCommand mycom = new MySqlCommand(sql,mycoon);
  mycoon.Open();
  int cont =Convert.ToInt32(mycom.ExecuteNonQuery());
  if (cont>0)
  {
  Session["user_account"] = Request.Form["textfield"].ToString();
  mycoon.Close();
  Response.Write("<script>alert('登錄成功');location='mina.aspx'</script>");
   
   
  }
  else
  {
  Response.Write("<script>alert('登錄失敗');location='login.aspx'</script>");
  }
  }
 

------解决方案--------------------
mycom.ExecuteScalar ()
------解决方案--------------------
你这个SQL语句根本没用到啊
------解决方案--------------------
探讨
你这个SQL语句根本没用到啊

------解决方案--------------------
你数据库里面的密码是MD5加密的吗
------解决方案--------------------
肯定是sql语句的问题了...
------解决方案--------------------
表名user加上中括号试试[user]尽量避免用user ,name这些命名
------解决方案--------------------
ExecuteNonQuery(),你这个用错了,登录返回的是bool你的是查询语句,你这个是执行插入和修改删除的!
------解决方案--------------------
自己一步步调试下 啊?
------解决方案--------------------
怀疑是你数据库设计的有问题,你是不是数据库里面这2个字段用的是char类型的,而且长度都比你输入的帐号的长度要长。
------解决方案--------------------
改表名,ExecuteScalar吧,别的看不出来了
------解决方案--------------------
C# code

if (Request.HttpMethod=="POST")
{
  string name = Request.Form["textfield"].ToString();
  string pwd = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Request.Form["textfield2"].ToString(),"MD5");
  string sql = "select * from user where user_account='"+name+"' and user_pwd='"+pwd+"'";
  string str = "server=localhost;user id=root;database=tfdo;password=97381687";
  MySqlConnection mycoon = new MySqlConnection(str);
  MySqlCommand mycom = new MySqlCommand(sql,mycoon);
  mycoon.Open();
  bool cont =mycom.ExecuteScalar();
  if (cont)
  {
  Session["user_account"] = Request.Form["textfield"].ToString();
  mycoon.Close();
  Response.Write("<script>alert('登錄成功');location='mina.aspx'</script>");
   
   
  }
  else
  {
  Response.Write("<script>alert('登錄失敗');location='login.aspx'</script>");
  }
  }