繁体   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