使用OleDbDataAdapter插入一个Access数据库
-
16-09-2019 - |
题
我想插入一些数据纳入一个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.
系统。数据。OleDb.OleDbException
信息:语法错误中插入的发言。
资料来源:Microsoft喷数据库引擎。
如果我改变插入命令:
da.InsertCommand = new OleDbCommand("INSERT INTO test ([text]) VALUES (?)");
和改变进入的数据只有一个单一的文本的价值,我获得:
没有任何价值给予一个或多个所需的参数。
我失去了一些东西?
解决方案
的问题是在数据类型。在问题的代码工作,如果数据类型是兼容的。
其他提示
- 确保所有需要列包括在插入的查询。
如果它不工作然后创建一个新的方法插入和按照这样的:
OleDbConnection conn=new OleDbConnection("连接字符串");
OleDbCommand command = new OleDbCommand(); command.Connection = conn; command.CommandText= "INSERT INTO myTable (col1, col2) VALUES (@p_col1, @p_col2)"; command.Parameters.Add ("@p_col1", OleDbType.String).Value = textBox1.Text; ... command.ExecuteNonQUery();
不隶属于 StackOverflow