Compress and repair an Access database in C # (translated)

Source: Internet
Author: User
Tags reflection access database
access| Data | database | compression

Introduced

The following C # code can be used to compress and repair an Access database, whether it's a simple ". mdb" or a ". MDW" network shared database, and the process and the tools-database utility-compression and repair that you use in MS Access applications Performs exactly the same operation. The instance code uses "late binding" (the runtime builds a COM object in memory) so that there is no need to add COM references to the project or to install MS Access applications on the PC. Only one jet engine is required (the Jet engine is included in the MDAC installation package, In a later version of Windows NT4, the system has already brought this engine on its own.

Background

I don't know if you're tired of adding complex COM library references to projects, but I believe this is pure. NET code eliminates additional interaction, RCWS, and COM references. Basically, because of the different Microsoft class libraries installed in the system (for example, MS Office Object library 9,10,11, and so on), We do not know the version of Office that is installed in the user's PC, so we have to access COM objects via ProgID instead of CLSID. For example, when you call " Excel.Application ", you get Excel, regardless of the version of MS Office installed in your system, when you add the " MS Excel Object Library refers to the fact that only a very restrictive feature is added to the application. So we use System.Reflection和迟绑定.

1. 实例代码

只需调用CompactAccessDB函数即可压缩和修复目标数据库.

2. 参数:

    • connectionstring– is used to connect to an Access database.
    • mdwfilename– the full name (path + filename) of the MDB file to compress.

Because of the limitations of the jet engine, executing this method to compress an Access database will result in a new file, so we need to copy the new Access file to the destination to overwrite the original uncompressed file.

When calling this method, verify that the compressed database has no open connections.

The code is as follows:

<summary>

MBD Compact Method (c) 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

An MDB file with 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 a appropriate value

//or leave it as if you are 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 a 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;

}

Attention matters

Jet Engine 5 is used for the Jet4X database. Pay attention to the following table when using:

Jet Oledb:engine Type

Jet x.x Format MDB Files

1

JET10

2

JET11

3

Jet2x

4

Jet3x

5

jet4x



Original link: http://www.codeproject.com/cs/database/mdbcompact_latebind.asp



Related Article

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.