Click here to Skip to main content
15,845,829 members
Articles / Programming Languages / C#

Compact and Repair Access Database using C# and Late Binding

Rate me:
Please Sign up or sign in to vote.
4.80/5 (57 votes)
20 Apr 20212 min read 329.6K   104   2
The code compacts and repairs an MS Access database from C# code using a JRO.JetEngine COM-object. Binding occurs at runtime, no references, no interops. Pure System.Reflection.
In this article, you will find code that compacts and repairs an MS Access DB from a C# .NET application.


This code compacts and repairs an MS Access database from a C# .NET application, no matter whether it's a simple ".mdb", or a ".mdw"-secured workgroup-shared DB. It performs exactly the same operation as "Tools - Database Utils - Compact and Repair Database..." menu item in the MS Access application. The code uses "late binding" (creating COM-objects in memory at runtime), and that's why you don't need any annoying interop COM references in your project. You don't even need MS Office installed. Just make sure you have a Jet Engine (Jet is included in MDAC package, which comes with any Windows installation starting from NT 4).


Don't you hate COM-library references in .NET-projects? I believe that pure .NET-code has to be free of any interops, RCWs, and other referenced COM-stuff. Basically because there's a load of different versions of MS libraries (for example, MS Office Object Library 9, 10, 11 etc.). We never know what version of MS Office is installed on a client machine, that's why we should access a COM-object via ProgID, and not CLSID. For example: you want to be sure that when you call for an "Excel.Application", you get Excel, no matter what version of MS Office is installed. And when you add a reference "MS Excel 10 Object library", you add a strong limitation to your software! So... use System.Reflection and late binding.

Using the Code

Just call a method CompactAccessDB. This method compacts and repairs your database.


  • connectionString - connection string to your database
  • mdwfilename - is a full name (path+name) of an MDB-file you want to compact and repair

Due to Jet limitations, the method compacts your database to a new file, so we have to copy the new compacted file over an old one.

When you call this method, make sure that there's no open connections to your database. Stop your threads.

Now, to the code:

/// <summary>
/// MBD compact method (c) 2004 Alexander Youmashev
/// !make sure there's no open connections
///    to your db before calling this method!
/// </summary>
/// <param name="connectionString">connection string to your db</param>
/// <param name="mdwfilename">FULL name
///     of an MDB file you want to compress.</param>
public static void CompactAccessDB(string connectionString, string mdwfilename)
    object[] oParams;

    //create an instance of a Jet Replication Object
    object objJRO = 

    //filling Parameters array
    //change "Jet OLEDB:Engine Type=5" to an appropriate value
    // or leave it as is if you db is JET4X format (access 2000,2002)
    //(yes, jetengine5 is for JET4X, no misprint here)
    oParams = new object[] {
        "Provider=Microsoft.Jet.OLEDB.4.0;Data" + 
        " Source=C:\\tempdb.mdb;Jet OLEDB:Engine Type=5"};

    //invoke a CompactDatabase method of a JRO object
    //pass Parameters array

    //database is compacted now
    //to a new file C:\\tempdb.mdw
    //let's copy it over an old one and delete it

    System.IO.File.Move("C:\\tempdb.mdb", mdwfilename);

    //clean up (just in case)

Points of Interest

Interesting, that Jet Engine 5 is used for JET4X databases. Be careful. See the table:

Jet OLEDB:Engine Type Jet x.x Format MDB Files
1 JET10
2 JET11


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


  • 20th April, 2021: Initial post

Written By
Founder Jitbit
United Kingdom United Kingdom
I'm CEO/Founder and lead developer at Jitbit Software. My personal blog is here

Comments and Discussions

QuestionDoes Not Work Pin
llahman11-Aug-23 12:52
professionalllahman11-Aug-23 12:52 
QuestionClarification Pin
Andrew Truckle25-May-22 1:52
professionalAndrew Truckle25-May-22 1:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.