Roger
Roger

Reputation: 1631

Microsoft Access Compact and Repair using C# .accdb files

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."}

Upvotes: 1

Views: 5583

Answers (1)

Gord Thompson
Gord Thompson

Reputation: 123399

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);
}

Upvotes: 3

Related Questions