David Smith

David Smith

  • NA
  • 2k
  • 0

Compact access database

Nov 17 2010 8:55 AM
     Hi i'm compacting access database for some reason when i get to the invoke method below via the strConnection, it does not understand the |DataDirectory|\Database.mdb path
    but if say  "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\Database.mdb" it will find the database, right now with connection string below the error says  not a valid file name

    string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Database.mdb";

     public static void CompactAccessDB(string connectionString, string mdwfilename)
    {
      object[] oParams;
      //create an inctance of a Jet Replication Object
    
      object objJRO =
        Activator.CreateInstance(Type.GetTypeFromProgID("JRO.JetEngine"));
    
      //filling Parameters array
      oParams = new object[] {
                                connectionString,   //Database location you compacting
                                "Provider=Microsoft.Jet.OLEDB.4.0;Data" +
                                " Source=C:\\Temp\\Database.mdb;Jet OLEDB:Engine Type=5"  //location you are moving the database to
                             };
   
      //invoke a CompactDatabase method of a JRO object pass Parameters array
      objJRO.GetType().InvokeMember("CompactDatabase",
                                    System.Reflection.BindingFlags.InvokeMethod,
                                    null,
                                    objJRO,
                                    oParams);
    
      System.IO.File.Delete(mdwfilename);
      System.IO.File.Move("C:\\Database.mdb", mdwfilename);
    
      //clean up (just in case)
      System.Runtime.InteropServices.Marshal.ReleaseComObject(objJRO);
      objJRO = null;
    }

Answers (2)