title: Closing connection to Access DB with OleDb
body: <p>I have an Access Database I'm connecting to with OleDb.  Everything is working fine with connection and useage, but I need to make a backup of the file.  </p>
  
  <p>I am closing the connection:</p>
  
  <pre><code>  public class myDbHandlerClass
    {
  
      private OleDbConnection myConnection;
      //in reality this string gets built by properties to the class
      //it ends up being this...
      //Yes Jet 4.0 this is an Access 2003 database
      private string myConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=myDatabase.mdb";
  
      public void OpenDatabase()
      {
        //to open the database
        try
        {
          // Does a previous connection exist?
          if ((myConnection != null) &amp;&amp; myConnection.State != ConnectionState.Closed) return;
  
          //No database connection string is specified, can't continue
          if (string.IsNullOrEmpty(myConnectionString)) return;
  
          myConnection = new OleDbConnection(myConnectionString);
          myConnection.Open();
  
        }
        catch (Exception ex)
        {
          ExTrace.WriteLineIf(TraceLog.TraceError, ExTrace.ShowException(ex));
        }
      }
  
      public void CloseDatabase()
      {
        try
        {
          if ((myConnection == null)) return;
  
          if (myConnection.State != ConnectionState.Closed) myConnection.Dispose();
          myConnection = null;
          GC.Collect();
        }
        catch (Exception ex)
        {
          ExTrace.WriteLineIf(TraceLog.TraceError, ExTrace.ShowException(ex));
        }
      }
    }
  </code></pre>
  
  <p>No exception is thrown, connection state == closed, myConnection == null, but the .ldb file never goes away. My subsequent code which is supposed to move the "myDatabase.mdb" file to "myDatabase.bak" fails because the file is already in use - by my program. </p>
  
  <p>How can I ensure it is actually closed and not locked. </p>
  
  <p>EDIT:
  I modified the code with suggestions from comments below and now it is working.</p>
  
  <pre><code>myConnection.Dispose(); 
  </code></pre>
  
  <p>And explicitly calling 
      GC.Collect() </p>
  
  <p>is what got it working.  </p>
  
  <p>Thanks for the help!</p>