Use C # To Compress and repair Access Database (zz)

Source: Internet
Author: User

Http://andy65007.cnblogs.com/archive/2005/09/11/234305.html
Introduction

The following section of C # code can be used to compress and fix the ACCESS database, whether it is a simple ". mdb "or ". MDW "network shared database, this process is exactly the same as the operation you performed when using" tool-database utility-compression and repair "in the MS Access application. the instance Code uses "late binding" (COM objects are created in the memory during running), so you do not need to add com references to the project, you do not need to install the MS Access Application on the PC. only one jet engine is required (the jet engine is included in the MDAC installation package. In Versions later than Windows NT4, the system has already provided this engine ).

Background

I wonder if you are tired of adding complicated com library references to the project, but I believe this is pure. net code will save additional interactive operations, 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, etc.), we do not know the office version installed in the user's PC, therefore, we need to use progid to access COM objects, instead of clsid. for example, when"Excel.Application", The obtained result is Excel, regardless of the MS Office version installed in the system, when the" MS Excel 10 Object Library "reference is added to the Code, in fact, it only adds a very restricted function to the application. so we use SYstem.ReflectionLate binding.

1. Instance code

You only need to callCompactAccessDBFunction can compress and repair the target database.

2. Parameters:

  • Connectionstring-used to connect to the access database.
  • Mdwfilename-Full name of the MDB file to be compressed (path + file name ).

Due to restrictions of the jet engine, executing this method to compress the ACCESS database will generate the result as a new file, therefore, we need to copy the new access file to the target location to overwrite the original uncompressed file.

When this method is called, make sure that the compressed database does not have an open connection.

The Code is as follows:

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

}

Notes

Jet Engine5For jet4X database. Pay attention to the following table when using it:

Jet oledb: Engine type

Jet x. x format MDB files

1

Jet10

2

Jet11

3

Jet2x

4

Jet3x

5

Jet4x

 

Original article: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.