private void wuzhonghao(string username, string pwd)
    {
        try
        {
            SqlConnection con = xzyywuliu.DB.createCon();
            con.Open();
            SqlCommand com = new SqlCommand("select userName,userPwd,IDCard from xzcx_login where userName='" + username + "' and userPwd='" + pwd + "'", con);
            SqlDataReader reader = com.ExecuteReader();
            if (!reader.HasRows)
            {
                this.Label1.Text = "用户名错误";
            }
            else
            {
                while (reader.Read())
                {
                    if (pwd != reader.GetString(1))
                    {
                        this.Label1.Text = "密码错误";
                    }
                    else
                    {
                        this.Panel2.Visible = true;
                        this.Panel1.Visible = false;
                        Session["user"] = reader["userName"];
                        Session["ID"] = reader["IDCard"];
                       
                    }
                }
            }
            if (con != null)
            {
                con.Close();
            }
        }
        catch
        { 
        
        }    }
为什么上面代码中,我把用户输入的是正确密码输入的是错的,这样就会提示密码错误 可是他不是提示密码错误而是提示用户错误这是为什么那

解决方案 »

  1.   

    if (!reader.HasRows)
                {
                    this.Label1.Text = "用户名错误";
                }=============
    reader.HasRows 用户名,密码错误一个。这里都是false前面一个! 。。就true了。。所以就用户名错误了。
      

  2.   

    楼主这样做会有缺陷,一般验证都是先拿来用户名在数据库中找,如果能找到再读出密码。
    然后拿这个密码和输入的密码进行比较。以防止SQL注入。