System.Data.OleDb.OleDbException需要一个或多个参数

时间:2017-06-28 03:22:42

标签: c# mysql sql database oledb

我'使用Visual Studio Enterprise 2015 WPF来执行我的项目,我的数据库是一个ms访问文件 我不确定为什么我有这个错误可以有人

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll Additional information: No value given for one or more required parameters.

这是我的代码

OleDbConnection con = new OleDbConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString();
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "select `Name1`,`ID` from `Employee` WHERE `Name1` = Jacob ";
cmd.Connection = con;
OleDbDataReader rd = cmd.ExecuteReader();
grid1.ItemsSource = rd;

我也试过

OleDbConnection con = new OleDbConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString();
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "select Name1,ID from [Employee] WHERE Name1 = Jacob ";
cmd.Connection = con;
OleDbDataReader rd = cmd.ExecuteReader();
grid1.ItemsSource = rd;


OleDbConnection con = new OleDbConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString();
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "select * from [Employee] WHERE Name1 = Jacob ";
cmd.Connection = con;
OleDbDataReader rd = cmd.ExecuteReader();
grid1.ItemsSource = rd;

但仍然是相同的错误

我的连接字符串

<connectionStrings>
<add name="Connection" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\wpfdb.accdb;Persist Security Info=False;"/>

1 个答案:

答案 0 :(得分:1)

在命令文本行中,例如:

> cmd.CommandText = "select `Name1`,`ID` from `Employee` WHERE `Name1` =
> Jacob ";

雅各布需要用引号括起来,而不是用列名和表名:

cmd.CommandText = "select * Employee WHERE Name1 = 'Jacob' ";