我没有输入任何帐户,但仍在登录


        private void btnLogin_Click(object sender, EventArgs e)
        {
            {
            Connections.con.Open();      
            string login = "SELECT ID, Username, [Password] FROM Employee";
            OleDbCommand command = new OleDbCommand(login, Connections.con);
            command.Connection = Connections.con;   
            command.Parameters.AddWithValue("@?", txtLUser.Text.ToString());
            command.Parameters.AddWithValue("@?", txtLPass.Text.ToString());
            OleDbDataReader reader = command.ExecuteReader();
            int count = 0;                
            while (reader.Read())
            {
                count = count + 1;
                break;
            }
            if (count == 1)
            {
                MessageBox.Show("Login Successful.");
                this.Close();
            }  
            else
            {
                MessageBox.Show("Please enter a valid Username or Password");
            }
            Connections.con.Dispose();
            Connections.con.Close();
            MessageBox.Show("Thank you for using this Simple Login/Registration Form.");
        }

每当我单击登录按钮时,它总是登录,我什至没有在用户/通行证文本框中键入任何内容,并且我的访问数据库中没有注册空白有什么建议吗?

您实际上并没有检查用户名和密码。 查看数据库查询:

"SELECT ID, Username, [Password] FROM Employee"

这将从Employee表中选择条记录。 然后检查这些记录:

while (reader.Read())
{
    count = count + 1;
    break;
}
if (count == 1)
{
    MessageBox.Show("Login Successful.");
    this.Close();
}

按照这个逻辑,只要Employee表中存在任何记录,登录就成功。

您可能只想检查与提供的凭据匹配的记录。 像这样:

"SELECT [ID], [Username], [Password] FROM [Employee] WHERE [Username] = @? AND [Password] = @?"

(我根据您添加参数的方式猜测参数语法,因为我不熟悉 MS Access 语法。 但希望你明白这个想法。

此外,这很重要,您似乎以纯文本形式存储用户密码。 这是一件极其可怕的事情。 请适当地对密码进行哈希处理,以便它们不能被读取为纯文本。

此外,您似乎正在使用共享连接对象:

Connections.con.Open();

这将导致一大堆问题。 在使用连接对象的方法范围内创建连接对象要简单得多,也更稳定。 基本上,连接对象应该在非常紧凑的范围内创建、使用和处置,并且不应泄漏到该范围之外。

你错过了属性的位置

string login = "SELECT ID, Username, [Password] FROM Employee where Username=@? and [Password]= @? ";

最新更新