1

I need to Compact and Repair .accdb (last MS Access) version using C#

I tried using this:

var jroEngine = new JRO.JetEngineClass();

var old_ = Provider=Microsoft.ACE.OLEDB.12.0;Data Source='c:\a.accdb';
var new_ = Provider=Microsoft.ACE.OLEDB.12.0;Data Source='c:\b.accdb';

jroEngine.CompactDatabase(old_, new_);
Marshal.ReleaseComObject(jroEngine);

There is an error:

{"Invalid argument."}

Roger Oliveira
  • 1,589
  • 1
  • 27
  • 55
  • 1
    What are the types of old_ and new_? I think they only need to contain the paths 'c:\a.accdb', etc. In any case, take a look at http://stackoverflow.com/questions/1866421/how-to-compact-msaccess-database-using-c-sharp for an example. – DWright Mar 23 '15 at 01:49
  • I have tried with only the paths and didn't work, it should be correct.. I think it is the version or something like that :/ – Roger Oliveira Mar 23 '15 at 03:13
  • see the [link](http://www.codeproject.com/Articles/7775/Compact-and-Repair-Access-Database-using-C-and-lat) the process better described here http://www.codeproject.com/Articles/7775/Compact-and-Repair-Access-Database-using-C-and-lat – nazark Mar 25 '15 at 08:02

1 Answers1

3

This is probably the most straightforward way to do it:

string sourceDbSpec = @"C:\Users\Public\a.accdb";
string destinationDbSpec = @"C:\Users\Public\b.accdb";

// Required COM reference for project:
// Microsoft Office 14.0 Access Database Engine Object Library
var dbe = new Microsoft.Office.Interop.Access.Dao.DBEngine();
try
{
    dbe.CompactDatabase(sourceDbSpec, destinationDbSpec);
}
catch (Exception e)
{
    Console.WriteLine("Error: " + e.Message);
}
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418