0

我正在尝试使用 OLEDB 替换 Excel 2007 中的表格。

首先,我正在执行命令“Drop Table”,而不是“Create Table”,它工作正常。

但是如果我现在想将数据插入("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++;
}
4

1 回答 1

0

您在事务中有 INSERT 查询,但我在您的代码中的任何地方都看不到提交:

execute.ExecuteNonQuery();
transaction.Commit();

您可能还应该transaction.Rollback();在您的异常块中有。请参见OleDbConnection.BeginTransaction 方法

于 2013-05-29T13:43:39.380 回答