Use C # To Compress and repair Access databases

Source: Internet
Author: User
The following section of the C # code can be used to compress and fix the Access database, whether it is a simple one. the mdbACCESS database is still one. the mdw network shares the database. This process is exactly the same as the one you used in the MSAccess application-database utility-compression and restoration. the instance Code uses late binding (running in memory)

The following section of the C # code can be used to compress and fix the Access database, whether it is a simple one. the mdbACCESS database is still one. the mdw network shares the database. This process is exactly the same as the one you used in the MS Access Application-database utility-compression and repair. the instance Code uses late binding (running in memory)

Introduction

The following section of C # code can be used to compress and fix the Access database, whether it is a simple ". mdb "ACCESS database 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 ).

C # background of Database ACCESS Operations

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 version of MS Office 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. reflection and late binding.

1. instance code

You only need to call the CompactAccessDB function to 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.
/** // MBD compact method (c) 2004 Alexander youmasev
///!! IMPORTANT !!
///! Make sure there's no open connections
/// To your db before calling this method!
///!! IMPORTANT !!
///
///
Connection string to your db
/**////
FULL name
/** // Of an MDB file you want to compress.
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;
}

,

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.