This code compacts and repairs an MS Access database from a C# .NET application

來源:互聯網
上載者:User

Introduction

This code compacts and repairs an MS Access database from a C# .NET application, no matter if 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).

Background

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.

Parameters:

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

/// !!IMPORTANT!!

/// !make sure there's no open connections

///    to your db before calling this method!

/// !!IMPORTANT!!

/// </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 inctance of a Jet Replication Object

    object objJRO = 

      Activator.CreateInstance(Type.GetTypeFromProgID("JRO.JetEngine"));


    //filling Parameters array

    //cnahge "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[] {

        connectionString,

        "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

    objJRO.GetType().InvokeMember("CompactDatabase",

        System.Reflection.BindingFlags.InvokeMethod,

        null,

        objJRO,

        oParams);


    //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.Delete(mdwfilename);

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


    //clean up (just in case)

    System.Runtime.InteropServices.Marshal.ReleaseComObject(objJRO);

    objJRO=null;

}


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

3 JET2X

4 JET3X

5 JET4X

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.