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

Source: Internet
Author: 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 ". 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 supported 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 shoshould access a COM-object via ProgID, and not CLSID. for example: you want to be sur E, 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 youmasev

///!! 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.