SqlConnection con = new SqlConnection("server=.;database=users;Trusted_Connection=true;");
        con.Open();
        SqlCommand cmd = new SqlCommand("select Acc,Pass from admin where Acc='" + TextBox1.Text + "' or Pass='" + TextBox2.Text + "'", con);
        SqlDataReader rd = cmd.ExecuteReader();
        if (rd.Read())
        {
            if (rd.GetValue(0).ToString() == TextBox1.Text)
            {
                if (rd.GetValue(1).ToString() == TextBox2.Text)
                {
                    Response.Redirect("Default.aspx");
                }
                else
                {
                    Response.Write("<script>alert('密码错误!')</script>");
                }
            }
            else
            {
                Response.Write("<script>alert('用户名错误!')</script>");
            }
        }
        else
        {
            Response.Redirect("Info.aspx");
        }我按照数据库里的数据来填,明明都写对了,但老是提示用户名错误,怎么回事?

解决方案 »

  1.   

    你跟踪一下看看“rd.GetValue(0).ToString() == TextBox1.Text”是否为true?
      

  2.   

    SqlCommand cmd = new SqlCommand("select Acc,Pass from admin where Acc='" + TextBox1.Text + "' and Pass='" + TextBox2.Text + "'", con); 
      

  3.   

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    就是这样而已哦。。
      

  4.   

    这个SQL语句应该改下
    SqlCommand cmd = new SqlCommand("select Acc,Pass from admin where Acc='" + TextBox1.Text + "' or Pass='" + TextBox2.Text + "'", con); 
    改成
    SqlCommand cmd = new SqlCommand("select Acc,Pass from admin where Acc='" + TextBox1.Text + "' and Pass='" + TextBox2.Text + "'", con); 下面就可以判断了
    if(dr.read())
    {
    response.write "登陆成功"
    }
    else
    {
    response.write "用户名或密码错误"
    }
      

  5.   

    哈哈,我也觉得是。。这个代码是我从网站上找的,现在看了kei的代码觉得那网站上的做法真逗
      

  6.   

    那你就先断点测试下TextBox1和TextBox2的值有没获得
    或者直接用
    response.write输出判断文本框的值有没获得然后再与数据库关联判断
      

  7.   

    kei的代码既简单又精准。。分都给你了哈