这是我做的一个登陆的代码,不管用户名和密码是正确还是错误都提示登陆失败。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语句根本没用到啊
[解决办法]
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>"); } }
[解决办法]
string sql = "select Count(*) from [user] where user_account='"+name+"' and user_pwd='"+pwd+"'";
..............
int cont =mycom.ExecuteScalar();
if(cont>0)
{
//登录成功
}
[解决办法]
进来看看是怎么回事,学习下
[解决办法]