OleDbDataAdapter .Fill() 方法给出了一个异常,说明没有为一个或多个必需参数给出值

OleDbDataAdapter .Fill() method gives an exception saying No value given for one or more required parameters

       `OleDbConnection connect = new OleDbConnection(connectionString);
        connect.Open();
        string lastName = tBoxSurname.Text;
        OleDbDataAdapter adapter = new OleDbDataAdapter(@"SELECT * FROM Players WHERE LastName=" + lastName, connect);

        DataSet dataset = new DataSet();
        adapter.Fill(dataset, "Players");

        dataGridView1.DataSource = dataset;
        dataGridView1.DataMember = "Players";
        connect.Close();`

我不明白为什么它总是出现这个问题。我寻找不同的解决方案并尝试了很多,但我一直遇到同样的问题。如果有人可以请解释发生了什么,因为我最近开始学习数据库。

试试这个:

OleDbDataAdapter adapter = new OleDbDataAdapter();
OleDbCommand command = new OleDbCommand("SELECT * FROM Players WHERE LastName = ?", connect);

command.Parameters.Add("LastName", OleDbType.VarChar).Value = tBoxSurname.Text;

adapter.SelectCommand = command;

请 post 下次将您的代码作为片段而不是图片! :)