title: OleDbConnection locks Excel xls file if the file is corrupted
body: <p>I have a legacy code that imports Excel (*.xls) to our db, then move the file to specific directory after processing.</p>
  
  <p>The code works fine except in one case, when the file is corrupted (even MS Excel cannot open it)! What happens in this case is that an <code>System.AccessViolationException</code> thrown at opening the connection!</p>
  
  <p>Here is how the code looks like:</p>
  
  <pre><code>        string connectionString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""", filePath);
          OleDbConnection connection = new OleDbConnection(connectionString);
          try
          {
              connection.ConnectionString = connectionString;
              connection.Open(); //&lt;&lt;&lt;--- exception throws here
              //file processing
          }
          catch (Exception e)
          {
              //exception handling
          }
          finally
          {
              connection.Close();
              connection.Dispose();
              connection = null;
              GC.Collect();
          }
  </code></pre>
  
  <p>Here is the exception details...</p>
  
  <pre><code>System.AccessViolationException was caught
    Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
    Source=System.Data
    StackTrace:
         at System.Data.Common.UnsafeNativeMethods.IDBInitializeInitialize.Invoke(IntPtr pThis)
         at System.Data.OleDb.DataSourceWrapper.InitializeAndCreateSession(OleDbConnectionString constr, SessionWrapper&amp; sessionWrapper)
         at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection)
         at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
         at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup)
         at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
         at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
         at System.Data.OleDb.OleDbConnection.Open()
  </code></pre>
  
  <p>As you can see, I am catching this exception and process it, then when the code try to move the file to another directory, I got the following exception:</p>
  
  <pre><code>System.IO.IOException occurred
    Message=The process cannot access the file because it is being used by another process.
    Source=mscorlib
    StackTrace:
         at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
         at System.IO.__Error.WinIOError()
         at System.IO.File.Move(String sourceFileName, String destFileName)
  </code></pre>
  
  <p>I tried to use another library, like <a href="https://github.com/paulyoder/LinqToExcel" rel="nofollow">LinqToExcel</a>, but found it internally uses the same implementation like mine, then it is has the same problem!</p>
  
  <p>I tried also to run garbage collector after the connection is closed (as you see in the above code) but faced the same problem!</p>
  
  <p>Any Idea?</p>