代码如下:
try
{
string str="Data Source=(local);Initial Catalog=MyLife;uid=;pwd=;Integrated Security=SSPI";
//string sql="select UserID,Pwd from User";
SqlConnection conn=new SqlConnection(str);
SqlCommand com=new SqlCommand("select * from UserTable",conn);
conn.Open();
SqlDataReader reader=com.ExecuteReader();
if(reader.Read())
{
if(reader["UserID"].ToString()==id.Text&&reader["Pwd"].ToString()==mima.Text)
{
MessageBox.Show("Welcome");
this.Close();
}
else
{
MessageBox.Show("你输入的用户名和密码不正确,请重新输入");
id.Text="";
mima.Text="";
}
}
conn.Close();
}
catch(SqlException   se)
{
MessageBox.Show(se.Message,"Warnning");
}
我只是验证数据库的密码,如果和数据库里面的一样就登陆成功,但是我输入数据库里面保存的信息总是执行("你输入的用户名和密码不正确,请重新输入")这句话,怎么这样的

解决方案 »

  1.   

    在ToString() 后边加上 .Trim() 试试
      

  2.   

    跟踪调试一下
    看看
    reader["UserID"].ToString()
    id.Text
    reader["Pwd"].ToString()
    mima.Text
    这些东西分别是什么
      

  3.   

    SQL语句改成"select * from UserTable where UserID='"+id.text+"' and Pwd='"+mima.text"'";
    后面的reader["UserID"].ToString()==id.Text&&reader["Pwd"].ToString()==mima.Text就直接写if(reader.Read())
    这样试试
      

  4.   

    string str="Data Source=(local);Initial Catalog=MyLife;uid=;pwd=;Integrated Security=SSPI";
    这句错了
    改成string str="Data Source=(local);Initial Catalog=MyLife;uid=你的sql用户名,sa;pwd=;Integrated Security=SSPI";
      

  5.   

    我的uid和pwd是空的,这个我以前用都是对的
      

  6.   

    不好意思,理解错了,你用的是
    if(reader.read())
    这个只会执行一次的,换句话说,如果你表中只有一个用户名和密码就会判断,但如果多了,第二条记录就不走了
      

  7.   

    换成
    bool bPass=false;
    while (reader.Read())
    {
      if(reader["UserID"].ToString()==id.Text&&reader["Pwd"].ToString()==mima.Text)
      {
       bPass = true;
      }
    }
    if (bPass==true)
    {
    MessageBox.Show("Welcome");
    this.Close();}
    else
    {
    MessageBox.Show("你输入的用户名和密码不正确,请重新输入");
    id.Text="";
    mima.Text="";
    }
      

  8.   

    更好的方法是
    SqlCommand com=new SqlCommand("select * from UserTable ",conn);
    改成
    SqlCommand com=new SqlCommand("select * from UserTable where UserID='"+id.Text.Trim()+"' and pwd='"+mima.Text.Trim()+"'",conn);
      

  9.   

    titcle,我按照你的方法成功了,谢谢你了,但是我不知道怎么给分,真的不好意思