Compress and repair an Access database in C #

Source: Internet
Author: User
Tags httpcontext reflection access database
access| Data | database | Compression INTRODUCTION

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 the COM object via ProgID instead of the CLSID. For example, when you call "Excel.Application", you get Excel, regardless of the installation of MS in the system The version of Office, when you add the "MS Excel Object Library" Reference to your code, simply adds a very restrictive feature to your application. So we use system.reflection and late binding.

1. Instance Code

You can compress and repair the target database by simply calling the COMPACTACCESSDB function.

2. Parameters:

    • 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>
Compressing a database
</summary>
<param name= "connectionString" > Database links </param>
<param name= "Mdwfilename" > The full name of the MDB file to compress </param>
private void Compactaccessdb (String connectionString, String mdwfilename)
{
Try
{
Object[] Oparams;

Object OBJJRO = Activator.CreateInstance (Type.gettypefromprogid ("JRO. JetEngine "));

Oparams = new object[] {connectionString, "Provider=Microsoft.Jet.OLEDB.4.0;Data" + "source=" + HttpContext.Current.Req Uest. Physicalapplicationpath + "TEMPDB.MDB; Jet oledb:engine type=5 "};

Objjro.gettype (). InvokeMember ("CompactDatabase", System.Reflection.BindingFlags.InvokeMethod, NULL, OBJJRO, oparams);

System.IO.File.Delete (Mdwfilename);
System.IO.File.Move (HttpContext.Current.Request.PhysicalApplicationPath + "Tempdb.mdb", mdwfilename);

System.Runtime.InteropServices.Marshal.ReleaseComObject (OBJJRO);
OBJJRO = null;
}
catch (Exception)
{
Response.Write ("There is an error in database compression!) ");
}
}

Attention matters

Jet Engine 5 is used for jet4x databases. 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



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.