SqlConnection thisConnection = new SqlConnection(@"Server=.;Database=master; Integrated Security=True;");
                thisConnection.Open();
                SqlCommand thisCommand = thisConnection.CreateCommand();
                thisCommand.CommandText = "Select userName,password from userInf";
                SqlDataReader thisReader = thisCommand.ExecuteReader();
                //验证用户名和密码
                while (thisReader.Read())
                {
                    if (thisReader["userName"].ToString() == userNameTextBox.Text)
                    {
                        if (thisReader["password"].ToString() == passwordTextBox.Text)
                        {
                            Form2 frm = new Form2();
                            frm.ShowDialog();
                        }
                        else
                        {
                            MessageBox.Show("密码错误,请重新输入!", "登陆", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            passwordTextBox.Text = "";
                            this.passwordTextBox.Focus();
                        }
                    }
                    else
                    {
                        MessageBox.Show("该用户名不存在,请重新输入!", "登陆", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        userNameTextBox.Text = "";
                        this.userNameTextBox.Focus();                    }
  上述写法在验证时循环有问题,在SQL表中循环验证输入的用户名和密码该如何写法?

解决方案 »

  1.   

    查询时用SQL加WHERE条件检索
    即把你上面的SQL语句 + Where 用户名/密码 =TextBox的值
    然后thisReader.Read()就可以知道有没有记录
      

  2.   

    bool bl = false;
      while (thisReader.Read()) 
                    { 
                        if (thisReader["userName"].ToString() == userNameTextBox.Text) 
                        { 
                            if (thisReader["password"].ToString() == passwordTextBox.Text) 
                            { 
                                Form2 frm = new Form2(); 
                                frm.ShowDialog(); 
                                bl = true;
                            } 
                        } 
    }if(!bl )
    {
    MessageBox.Show("密码错误,请重新输入!", "登陆", MessageBoxButtons.OK, MessageBoxIcon.Information); 
                            this.userNameTextBox.Text = ""; 
                            this.userNameTextBox.Focus(); }
      

  3.   


    SqlConnection thisConnection = new SqlConnection(@"Server=.;Database=master; Integrated Security=True;"); 
    thisConnection.Open(); 
    SqlCommand thisCommand = thisConnection.CreateCommand(); 
    thisCommand.CommandText = "Select count(*) from userInf where userName='" + userNameTextBox.Text + "' and password='" + passwordTextBox.Text + "'"; 
    int count=Convert.ToInt32(command.ExecuteScalar());  //command.ExecuteScalar()返回查询结果的第一行第一列,忽略其他的
    if(count==1)
    {
    //成功
    }
    else
    {
    //姓名密码错误
    }//如果你只查询姓名的话 查询语句的条件就跟一个姓名就可以了
      

  4.   

    SqlDataReader usdr = FPara.SqlReader("select top 1 * from lampUserName where UserName='" + UserCode + "'", FPara.connStr);
                        if (usdr != null)
                        {
                            if (usdr.Read())
                            {
                                if (usdr["Pswd"].ToString() != UserPswd)
                                {
                                    MessageBox.Show("密码错误");
                                }
                                else
                                {
                                   
                                    FPara.ShopCode = usdr["UserName"].ToString();
                                    MessageBox.Show("登录成功");
                                    this.Hide();
                                    Form4 f = new Form4();
                                    f.Show();
                                                                }
                            }
                            else
                            {
                                MessageBox.Show("帐号密码错误");
                            }
                            usdr.Close();
                        }
                        else
                        {
                            MessageBox.Show("无法连接数据库,请检查相关环境");
                            Application.Exit();
                            this.Close();
                        }