0

我有一个 sql 数据库,其中包含用户名和密码列。我制作了登录表单,要求用户输入有效的用户名和密码。

错误条目的检查已经有效,但用户名密码组合正确时的检查失败。

namespace Kartice
{
public partial class Pass : Form
{

    Matjaz Matjaz = new Matjaz();
    public Pass()
    {
       // string myconnection = @"C:\Users\Bojan\Desktop\Programiranje\School\Kartice\Kartice\Users.mdf";
        InitializeComponent();
    }

    private void usersBindingNavigatorSaveItem_Click(object sender, EventArgs e)
    {

        this.Validate();
        this.usersBindingSource.EndEdit();
        this.tableAdapterManager.UpdateAll(this.usersDataSet);

    }

    private void Pass_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'usersDataSet.Users' table. You can move, or remove it, as needed.
        this.usersTableAdapter.Fill(this.usersDataSet.Users);

    }

    private void BtnOk_Click(object sender, EventArgs e)
    {
            if (Imetxt.Text == "")
            {
                MessageBox.Show("Please enter a valid user name!");
                Imetxt.Focus();
            }
            else if (Passtxt.Text == "")
            {
                MessageBox.Show("Please enter a valid password!");
                Passtxt.Focus();
            }

            string userName1, userPassword1;
            userName1 = Imetxt.Text;
            userPassword1 = Passtxt.Text;

            foreach (DataRow row in usersDataSet.Users)
            {
                if (row["Ime_Priimek"].ToString() == userName1)
                {
                    if (row["Password"].ToString() == userPassword1)
                    {
                        Pass myForm1 = new Pass();
                        Matjaz myForm2 = new Matjaz();

                        myForm2.Show();
                        myForm1.Hide();
                        break;
                    }
                }
                else
                    MessageBox.Show("You have entered a wrong user name or the password!");
            }
}
4

2 回答 2

1

MessageBox是在循环for

假设我们在表中有 5 个用户。

假设我输入luke了用户名和正确的密码。

=======================
| Username | Password |
|- - - - - | - - - - -|
| matthew  |  *****   |
| mark     |  *****   |
| luke     |  *****   |
| john     |  *****   |
=======================

用户luke名在此表中排在第三位。

这是您的程序所做的,用简单的英语:

<<start>>
Is 'matthew' equal to 'luke'?
- No! Display a message box.

Is 'mark' equal to 'luke'?
- No! Display a message box.

Is 'luke' equal to 'luke'?
- Yes!
  - Is '****' equal to '****'?
    - Yes! Show Form2 and hide Form1.
<<end>>

希望问题现在很清楚。您需要做的第一件事是将循环MessageBox移出。for

bool userFound = false;
foreach (DataRow row in usersDataSet.Users)
{
    if (row["Ime_Priimek"].ToString() == userName1)
    {
        if (row["Password"].ToString() == userPassword1)
        {
            userFound = true;
            Pass myForm1 = new Pass();
            Matjaz myForm2 = new Matjaz();

            myForm2.Show();
            myForm1.Hide();
            break;
        }
    }
}

if (!userFound)
{
    MessageBox.Show("You have entered a wrong user name or the password!");
}
于 2013-11-14T15:00:47.743 回答
0

忽略明文密码的所有其他问题并将数据库下载到客户端等......

您的消息框位于 for 循环内,因此每个不匹配的用户都会显示 MessageBox...。

将 for 循环更改为

foreach (DataRow row in usersDataSet.Users)
{
  if (row["Ime_Priimek"].ToString() == userName1 && row["Password"].ToString() == userPassword1)
    {
      Pass myForm1 = new Pass(); // Don't think you need this.
      Matjaz myForm2 = new Matjaz();

      myForm2.Show();
      myForm1.Hide(); // shouldn't this be this.Hide();
      return;
    }
  }
}
MessageBox.Show("You have entered a wrong user name or the password!");

更新

与其他评论/答案内联

为了防止在用户名或密码为空时进行用户检查,在每次焦点调用后插入一个返回语句。上面的代码修复了它不会对您现有的问题产生影响,但我认为它更接近您的预期执行。例如

        if (Imetxt.Text == "")
        {
            MessageBox.Show("Please enter a valid user name!");
            Imetxt.Focus();
            return;
        }
        else if (Passtxt.Text == "")
        {
            MessageBox.Show("Please enter a valid password!");
            Passtxt.Focus();
            return;
        }
于 2013-11-14T14:59:14.997 回答