簡體   English   中英

“用戶名或密碼不正確” - 收到相同的消息

[英]"Username or Password is not correct" - getting the same message

我試圖制作一個登錄頁面。 但是,它總是顯示此消息:

用戶名或密碼不正確

雖然我很確定我的用戶名和密碼是正確的。

private void button1_Click(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(txtbox_user.Text) || string.IsNullOrEmpty(txtbox_password.Text))
    //txt_name.Text == String.IsNullOrEmpty || txt_family.Text == String.IsNullOrEmpty || txt_username.Text == String.IsNullOrEmpty || txt_password.Text == String.IsNullOrEmpty || txt_repeat.Text == String.IsNullOrEmpty || txt_mail.Text == String.IsNullOrEmpty)
    {
        MessageBox.Show("Plesae insert your username and password");
    }
    else
    {
        connection.Open();
        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;
        command.CommandText = " select * from Login where UserName =' " + txtbox_user.Text + " ' and PassWord = ' " + txtbox_password.Text + " ' ";
        OleDbDataReader reader = command.ExecuteReader();
        int count = 0;
        while (reader.Read())
        {
            count = count + 1;
        }
        if (count == 1)
        {
            MessageBox.Show("Welcome!");
            LearnerForm F2 = new LearnerForm();
            F2.Show();
        }
        if (count > 1)
        {
            MessageBox.Show("Duplicate username or Password");
        }
        else
        {
            MessageBox.Show("Username or Password is not correct");
        }

        connection.Close();
    }
}

在引號'后有一個額外的空格。 去掉它:

where UserName =' " + txtbox_user.Text + " ' and PassWord = ' " + txtbox_password.Text + " '

應該:

where UserName ='" + txtbox_user.Text + "' and PassWord = '" + txtbox_password.Text + "'

此外,我強烈建議您始終使用參數化查詢來避免SQL 注入

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM