MySQLCommand 对象使用带条件的where查询语句时, 为什么ExecuteReader()方法不返回任何数据?
一、问题描述:
使用vs编写web程序时,发现MySqlcommand对象中的ExecuteReader()方法返回数据有误。只有查询语句不带where条件时才能正常返回,带有where条件时不返回数据。
二、代码
//打开数据库的连接
conn.Open();
//SQL查询语句
sql = "SELECT user,password FROM tbl_user WHERE user=\"李清悦\"";
//查询
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader rdr = cmd.ExecuteReader();
//密码验证
if (rdr.Read())
{
//比较数据
if (txtPassword.Text == rdr["password"].ToString())
{
//将用户名存入Session变量user
Session["user"] = rdr["password"].ToString();
//重定向
Response.Redirect("Welcome.aspx");
}
}
else Response.Write("<script>alert('该用户不存在!!!')</script>");
//关闭连接
conn.Close();