OLEDB将行插入替换表中

时间:2013-05-29 10:16:59

标签: c# .net oledb

我正在尝试使用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++;
}

1 个答案:

答案 0 :(得分:0)

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

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

您的异常块中可能还应该有transaction.Rollback();。见OleDbConnection.BeginTransaction Method