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();
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!