使用 OleDbDataAdapter 插入 Access 数据库
我想将一些数据插入到 Access 数据库中。
DataTable dt = new DataTable();
String sql = string.Format("SELECT * FROM {0} where 1=0; ", tmap.SqlTableName);
string con = string.Format(conn, accessPath);
OleDbDataAdapter da = new OleDbDataAdapter(sql, con);
OleDbCommandBuilder cmdBuilder = new OleDbCommandBuilder(da);
da.InsertCommand = cmdBuilder.GetInsertCommand(true); // Returns "INSERT INTO test (int, bdate, amt, text, bit) VALUES (?, ?, ?, ?, ?)"
da.Fill(dt);
//Add data to the DateTable
for (int i = 0; i < rowCount; i++)
{
DataRow dr = dt.NewRow();
//....
dt.Rows.Add(dr);
}
da.Update(dt); //This is where things go south.
System.Data.OleDb.OleDbException
消息:INSERT INTO 语句中存在语法错误。
来源:Microsoft JET 数据库引擎。
如果我更改插入命令:
da.InsertCommand = new OleDbCommand("INSERT INTO test ([text]) VALUES (?)");
并将传入数据更改为只有一个文本值,我会得到:
没有为一个或多个必需参数给出值。
我错过了什么吗?
I would like to insert some data into an Access Database.
DataTable dt = new DataTable();
String sql = string.Format("SELECT * FROM {0} where 1=0; ", tmap.SqlTableName);
string con = string.Format(conn, accessPath);
OleDbDataAdapter da = new OleDbDataAdapter(sql, con);
OleDbCommandBuilder cmdBuilder = new OleDbCommandBuilder(da);
da.InsertCommand = cmdBuilder.GetInsertCommand(true); // Returns "INSERT INTO test (int, bdate, amt, text, bit) VALUES (?, ?, ?, ?, ?)"
da.Fill(dt);
//Add data to the DateTable
for (int i = 0; i < rowCount; i++)
{
DataRow dr = dt.NewRow();
//....
dt.Rows.Add(dr);
}
da.Update(dt); //This is where things go south.
System.Data.OleDb.OleDbException
Message: Syntax error in INSERT INTO statement.
Source: Microsoft JET Database Engine.
If I change the insert command:
da.InsertCommand = new OleDbCommand("INSERT INTO test ([text]) VALUES (?)");
and change the incoming data to only have a single text value I get:
No value given for one or more required parameters.
Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果它不起作用,则创建一个新的插入方法并按照以下步骤操作:
OleDbConnection conn = new OleDbConnection(connectionString);
If It doesn't work then create a new method for inserting and follow this:
OleDbConnection conn = new OleDbConnection (connectionString);
问题出在数据类型上。如果数据类型兼容,问题中的代码将有效。
The issue was in the data types. The code in the question works if the data types are compatible.