为什么在更新Excel单元格多次时收到OleDbException?

我正在使用下面的代码来更新Excel文件中的单元格。

public bool WriteChange(string Filename, string SheetName, string Cell, string Value) { if(!System.IO.File.Exists(Filename)) { throw new System.IO.FileNotFoundException("File \"" + Filename + "\" could not be found"); } bool result = false; string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Filename + ";Mode=ReadWrite;Extended Properties=\"Excel 8.0;HDR=NO;\""; string SQL = "UPDATE [" + SheetName + Cell + ":" + Cell + "] SET F1='" + Value + "'"; using(OleDbConnection Connection = new OleDbConnection(ConnectionString)) { Connection.Open(); using(OleDbCommand cmd = new OleDbCommand(SQL,Connection)) { int value = cmd.ExecuteNonQuery(); if(value > 0) { result = true; } } } return result; } 

哪个工作正常,除非我尝试多次更新同一个单元格。 一旦一个单元格被更新使用这个function,它永远不会再使用这个function来更新。 如果我尝试再次更新单元格; 即使重新启动应用程序后,我得到一个OleDbException: System Resource Exceeded

我知道你通常会收到这个exception,如果你正在创build一堆到电子表格的连接(例如在一个循环中),但是我只在每次应用程序运行时连接一次。 典型的工作stream程是。

  • 开始申请。
  • 调用WriteChange
  • 退出应用程序

为什么我得到这个错误,当连接重新连接的时候连接应该已经很长时间了?

使用以下连接string,因为Microsoft.Jet.OLEDB.4.0有此错误。

 string ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + @Filename + ";Extended Properties=\"Excel 12.0;HDR=No;\""; 

我想这个错误可能是因为你没有closuresOleDbconnection连接而发生的。 完成更新查询后closuresDbconnection – int value = cmd.ExecuteNonQuery();