OLEDB将行插入到已替换的表中
本文关键字:替换 插入 OLEDB | 更新日期: 2023-09-27 18:15:56
我正在尝试使用OLEDB替换Excel 2007中的表。
首先我执行命令"删除表",然后"创建表",它工作得很好。
但是如果我现在想要插入数据("INSERT INTO")
到这个表中,它会失败。OleDbCommand.ExecuteNonQuery()
没有错误或异常,最后事务提交成功,数据库为空。
知道为什么吗?
connection.Open();
string access_com = "DROP TABLE " + globalPrefix + prefix + TableName;
OleDbCommand execute = new OleDbCommand(access_com, connection);
try
{
execute.ExecuteNonQuery();
}
catch (Exception ex)
{
ConfigDataSet.Log.AddLogRow("The program cannot drop table you want. Close the file with it and run program again!", 1);
return 1;
}
access_com = "CREATE TABLE [" + globalPrefix + prefix + TableName + "]" + fieldString + ")";// CONSTRAINT PK" + TableName + " PRIMARY KEY " + primaryKey + ")";
execute.CommandText = access_com;
execute.ExecuteNonQuery();
OleDbTransaction transaction = connection.BeginTransaction();
access_com = "INSERT INTO " + TableName + "( " + allfields + ")" + " VALUES (" + parametersString + ")";
OleDbCommand execute = new OleDbCommand(access_com, connection);
execute.Transaction = transaction;
try
{
execute.ExecuteNonQuery();
execute.Parameters.Clear();
}
catch (OleDbException ex)
{
ConfigDataSet.Log.AddLogRow("Inserting row failed: ", 2);
failedInsertions++;
}
您在事务中有INSERT查询,但我在代码中没有看到任何提交:
execute.ExecuteNonQuery();
transaction.Commit();
你可能也应该在你的异常块中有transaction.Rollback();
。看到OleDbConnection。BeginTransaction方法