我做一个用户登陆界面。按下登陆按钮后执行的代码如下:if (textBox1.Text == "" || textBox2.Text == "")
            {
                MessageBox.Show("请输入用户名和密码");
            }
            else
            {
              
                string s ="Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\c#程序 \\zhuce\\zhuce\\username.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
                SqlCommand da;
                SqlDataReader dr;
                SqlConnection conn = new SqlConnection(s);
                da = new SqlCommand("select * from password", conn);
                conn.Open();
                dr = da.ExecuteReader();
                while (dr.Read())
                {
                    string[] ss = new string[] { dr.GetString(0).Trim(), dr.GetString(1).Trim() };
                    if (ss[0] == textBox1.Text && ss[1] == textBox2.Text)
                    {
                        Form2 f = new Form2();
                        f.Show(); 
                    }
                };       
            }我想在密码错误时执行:messagebox。show(“密码或用户名不正确”);这句话该放哪里;怎么放。多谢

解决方案 »

  1.   


    if (textBox1.Text == "" || textBox2.Text == "")
      {
      MessageBox.Show("请输入用户名和密码");
      }
      else
      {
        
      string s ="Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\c#程序 \\zhuce\\zhuce\\username.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
      SqlCommand da;
      SqlDataReader dr;
      SqlConnection conn = new SqlConnection(s);
      da = new SqlCommand("select * from password", conn);
      conn.Open();
      dr = da.ExecuteReader();
      while (dr.Read())
      {
      string[] ss = new string[] { dr.GetString(0).Trim(), dr.GetString(1).Trim() };
      if (ss[0] == textBox1.Text && ss[1] == textBox2.Text)
      {
      Form2 f = new Form2();
      f.Show();  
      }
      else
      {
        Messagebox.Show(“密码或用户名不正确”);//此处提示错误
      }
      };   
      }
      

  2.   

    在这个地方:  if (ss[0] == textBox1.Text && ss[1] == textBox2.Text)
      {
      Form2 f = new Form2();
      f.Show();  
      }
    else
    {
    Messagebox.Show("密码或用户名不正确");
    }
      

  3.   

    不行的呀。我试过。就算正确他也会弹出messagebox,那个while循环还没结束。
      

  4.   

    if (ss[0] == textBox1.Text && ss[1] == textBox2.Text)
      {
      Form2 f = new Form2();
      f.Show();  
      }
    else
    {
    MessageBox.Show("密码或用户名不正确", "错误提示", MessageBoxButtons.OK); 
    }
      

  5.   

    大家看清楚,这有个while循环啊。就算第一次对了。他还要读数据库里的数据来比较的。可是我不知道在那里break掉while循环才合适
      

  6.   

    da = new SqlCommand("select * from password ", conn);
    select 语句要加条件的,where username='' and password=''而且你需要用dr.read
    用int count=(int)cmd.ExecuteNonQuery();
    然后判断count=0的时候,弹提示
      

  7.   

                    string uname = this.txtUserName.Text.ToString();
                    string upassword = this.txtPsw.Text.ToString();
                    SqlConnection thisconnection = new SqlConnection(@"Server=.;database=brmine;uid=sa;pwd=123456");
                    thisconnection.Open();
                    SqlCommand thiscommand = thisconnection.CreateCommand();
                    thiscommand.CommandText = "select count(*) from UserLogin where UserName='" + txtUserName.Text.Trim() + "' and Passwd='" + txtPsw.Text.Trim() + "'";
                    SqlDataReader thisreader = thiscommand.ExecuteReader();
                    if (thisreader.Read())
                    {
                        if (thisreader["password"].ToString().Trim() == upassword)
                        {
                            MessageBox.Show("恭喜您登陆成功!", "登陆成功!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            brMainInfoManage tt = new brMainInfoManage();
                            tt.Show();                    }
                        else { MessageBox.Show("密码错误,请重新输入!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information); }                }
                    else { MessageBox.Show("此用户不存在,请您注册!", "注册", MessageBoxButtons.OK, MessageBoxIcon.Information); }
                    thisconnection.Close();
                    thisreader.Close();
      

  8.   

    不需要用循环,不需要用dr.read
      

  9.   

    select count(*) from password where username='?' and password='?'
      

  10.   

    select count(*) from password where username='?' and password='?'
      

  11.   

    if (ss[0] == textBox1.Text && ss[1] == textBox2.Text)
      {
      Form2 f = new Form2();
      f.Show();  
     break;
      }
    else
    {
    MessageBox.Show("密码或用户名不正确", "错误提示", MessageBoxButtons.OK);  
    }
      

  12.   


    那就改成下面这样:if (dr.Read())
      {
      string[] ss = new string[] { dr.GetString(0).Trim(), dr.GetString(1).Trim() };
      
      if (ss[0] == textBox1.Text && ss[1] == textBox2.Text)
      {
      Form2 f = new Form2();
      f.Show();  
      }
      else
      {
        MessageBox.Show("密码或用户名不正确!", "错误提示", MessageBoxButtons.OK);
      }
      }
    else
    {
    MessageBox.Show("用户名不存在,请重新输入!", "错误提示", MessageBoxButtons.OK);
    }
      
    }
      

  13.   

    bool find= 0;
    while (dr.Read())
      {
      string[] ss = new string[] { dr.GetString(0).Trim(), dr.GetString(1).Trim() };
      if (ss[0] == textBox1.Text && ss[1] == textBox2.Text)
      {
      Form2 f = new Form2();
      f.Show(); 
    find = 1;
    break; 
      }
      }
    if(find == 0)
    Messagebox.Show("密码或用户名不正确");
      

  14.   

    错了,是int find = 0ly320老兄,你的if else是不是会让楼主拼命的弹出提示来阿
      

  15.   

    个人认为应该把输入的用户名和密码做为Sql语句的条件:string sql = "select * from Users where LoginId=@LoginId and LoginPwd=@LoginPwd";
    如果查出结果了就显示成功,如果受影响行数为0就显示失败(提示密码错误了 )