C# 登录表单没有';行不通

C# 登录表单没有';行不通,c#,login,C#,Login,我有这个登录代码,如果数据库中只有一个用户,它可以工作,但如果我添加另一个用户,然后使用该帐户登录,则会弹出消息“错误的用户名或密码”,但当我单击“确定”时,它会将我重定向到Form1 private void buttonLogin_Click(object sender, EventArgs e) { SQLiteConnection conn = new SQLiteConnection("data source = zivali.sqlite");

我有这个登录代码,如果数据库中只有一个用户,它可以工作,但如果我添加另一个用户,然后使用该帐户登录,则会弹出消息“错误的用户名或密码”,但当我单击“确定”时,它会将我重定向到Form1

 private void buttonLogin_Click(object sender, EventArgs e)
    {

        SQLiteConnection conn = new SQLiteConnection("data source = zivali.sqlite");
        conn.Open();
        SQLiteCommand com = new SQLiteCommand(conn);

        com.CommandText = "SELECT * FROM login;";

        SQLiteDataReader reader = com.ExecuteReader();

        while (reader.Read())
        {
            string username = reader["username"].ToString();
            string password = reader["password"].ToString();


                if (username == textBoxUserName.Text && password == textBoxPassword.Text)
                {
                    this.Hide();
                    Form1 f1 = new Form1();
                    f1.ShowDialog();
                }
                else
                {
                    MessageBox.Show("Wrong username or password");
                }

        }
        conn.Close();
    }

如果只有一个用户,while循环将循环一次。因此,while检查用户是否存在,如果不存在,它将显示一个消息框

如果您有多个用户。而while循环将运行X次。但是当它第一次循环并且凭证与给定的不匹配时,它将显示消息框

你说你已经添加了第二个用户。因此,当您使用第二个用户登录时,循环时的第一个用户将为false。这就是它显示消息框的原因

要解决此问题,您可以尝试以下方法:

       SQLiteConnection conn = new SQLiteConnection("data source = zivali.sqlite");
        conn.Open();
        SQLiteCommand com = new SQLiteCommand(conn);

        com.CommandText = "SELECT * FROM login;";

        SQLiteDataReader reader = com.ExecuteReader();

        // place your username/password decleration here, no need to read them X times in a loop
        string username = reader["username"].ToString();
        string password = reader["password"].ToString();

        // bool for if there is any user whith the given cridentials
        bool loginValid = false;
        while (reader.Read())
        {
            // if cridentials mathch set loginValid to true and break out of the loop 
            if (username == textBoxUserName.Text && password == textBoxPassword.Text)
            {
                loginValid = true;
                break;

            }
        }

        // check if the login is true
        if (loginValid)
        {
            this.Hide();
            Form1 f1 = new Form1();
            f1.ShowDialog();
        }
        else
        {
            MessageBox.Show("Wrong username or password");
        }
        conn.Close();

您不必在
语句中做出决定,而
语句中,您可能需要执行以下操作:

 bool doesMatch = false;
 string username = reader["username"].ToString();
 string password = reader["password"].ToString();


 while (reader.Read())
 {
       if (username == textBoxUserName.Text && password == textBoxPassword.Text)
      {
            doesMatch = true;
      }
 } 
 if (doesMatch)
 {
    this.Hide();
    Form1 f1 = new Form1();
    f1.ShowDialog();
 }
 else
 {
    MessageBox.Show("Wrong username or password");
 }

或者,您可以在查询中添加
WHERE
语句,以检查表中是否有一行用户的数据与输入的用户名和密码相同。

您的登录成功/失败代码将针对数据库中的每个登录运行。这就是为什么你会收到N-1个错误的用户名消息框和1个成功登录的原因。嗨。我尝试了您的代码,但现在我收到了以下错误消息InvalidOperationException:无当前行错误此处是指向错误图片的链接:我认为我犯了一个错误尝试移动这些行:
string username=reader[“username”]。ToString();字符串密码=读卡器[“密码”]。ToString()编码到while循环中。