title: Intermittent "System resource exceeded" exception for OleDB connection to Microsoft Access data file
body: <p>I'm experiencing a seemingly random "System resource exceeded" exception when running my code. The idea behind my program is that a third party piece of software is continuously writing data to a Microsoft Access database file (.res) - <em>about every 30 seconds</em> my code reads the data from this file, does some operations on it, and writes the results to our database.  Unfortunately I cannot change the way the third party software writes data to files, I am stuck with Access data files.</p>
  
  <p>This error occurs both on the production system running the WinForms program installed through Click-Once publishing as well as in a console test program on my development system.  I get the exception even when running a query that returns a single integer and no other program or thread is touching the file which is located on my local disk.</p>
  
  <p>Exception information:</p>
  
  <pre><code>System.Data.OleDb.OleDbException (0x80004005): System resource exceeded.
     at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
     at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object&amp; executeResult)
     at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object&amp; executeResult)
     at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object&amp; executeResult)
     at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
     at System.Data.OleDb.OleDbCommand.ExecuteScalar()
  ...
  </code></pre>
  
  <p>Example code that reproduces the problem:</p>
  
  <pre><code>string connectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\datafile.res";
  string commandText = "SELECT MIN(Data_Point) FROM Channel_Normal_Table WHERE Test_ID = 1";
  
  int connectionCounter = 0;            
  object result;
  
  while (true)
  {                
      using (OleDbConnection connection = new OleDbConnection(connectionString))
      {
          connection.Open();
          connectionCounter++;
  
          using (OleDbCommand command = new OleDbCommand(commandText, connection))
          {
              result = command.ExecuteScalar();
          }
  
          connection.Close();
      }
  }
  </code></pre>
  
  <p>Unfortunately the exception is not deterministic - I have seen it occur anywhere from the 4th command execution to the 6149th on the same file with the same code.  It always occurs on the command.ExecuteScalar() line.  If there is a resource leak in this code, please help me find it.</p>
  
  <p>I have tried installing the hotfix found at <a href="http://support.microsoft.com/kb/2760394" rel="nofollow">http://support.microsoft.com/kb/2760394</a> (and made the required registry change), but it does not solve the problem.  Any suggestions would be appreciated and aggressively pursued.</p>
  
  <p>This is running on Windows 7, C# 4.0 (console and WinForms), 4 GB RAM</p>