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

时间:2016-03-29 11:19:22

标签: c# oledbdataadapter

       `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();`

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

1 个答案:

答案 0 :(得分:1)

试试这个:

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;

请将您的代码下次发布为代码段,而不是图片! :)