试做登入小程序System.Data.OleDb.OleDbException:“至少一个参数没有被指定值。”

以下是代码



private void btn_Login_Click(object sender, EventArgs e)
        {
            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            command.CommandText = " select * from Table1 where Username='" + txt_Username.Text + "' and Password= '" + txt_Password.Text + "'";
            OleDbDataReader reader = command.ExecuteReader();
//System.Data.OleDb.OleDbException:“至少一个参数没有被指定值。”

            int count = 0;
            while (reader.Read())
            {
                count = count + 1;
            }
            if (count == 1)
            {
                MessageBox.Show("Username and Password are correct");
                connection.Close();
                connection.Dispose();
                this.Hide();
                Form2 f2 = new Form2();
                f2.ShowDialog();
            }
            else
            {
                if (count > 1)
                {
                    MessageBox.Show("Duplicate Username and Password");
                }
                else
                {
                    MessageBox.Show("Username and/or Password are NOT correct");
                }
            }
            connection.Close();
        }

检查表名称table1usernamepassword字段的拼写,肯定有一个有问题。要么就是连接到错误的的access数据库了,不存在table1表

有帮助麻烦点下【采纳该答案】,谢谢~~有其他问题可以继续交流~

打印一下connection对象,看看是否连接成功。