如何在.net环境中编程压缩Access数据库

博客介绍了如何使用Visual Basic.NET来压缩Microsoft Access数据库,包含了相关要求、构建示例的步骤以及可能遇到的陷阱等关键信息。

How to compact a Microsoft Access database by using Visual Basic .NET

SUMMARY

Neither ActiveX Data Objects (ADO) nor ADO .NET provide the means to compact or repair Microsoft Access databases. However, you can accomplish this task by using the Microsoft Jet OLE DB Provider and Replication Objects (JRO) that was introduced with Microsoft Data Access Components (MDAC) version 2.1. ADO .NET allows the use of COM-based object libraries through the Interop layer.

This article demonstrates how to compact an Access database by using Visual Basic .NET.


Requirements

Microsoft Visual Basic .NET
Microsoft Jet and Replication Objects 2.1, 2.5, or 2.6 Library

Steps to build example

1.Open a new Visual Basic .NET console application.
2.In the Solution Explorer window, right-click the References node and select Add Reference.
3.In the Add Reference dialog box, click the COM tab, and then select Microsoft Jet and Replication Objects 2.x Library. Click Select to add it to Selected Components. Click OK.

Note In this step, x is a placeholder for the actual version number of the Microsoft Jet and Replication Objects Library component.
4.A warning will be displayed if there is no wrapper found for the selected library. Click Yes to generate a wrapper. Microsoft ActiveX Data Objects Library (ADODB) and JRO references will be added to the project's References.
5.In the Solution Explorer window, right-click Module1.vb and click View Code.
6.Delete all of the code from the code window.
7.Copy the following code and paste it into the code window:
Module Module1

    Sub Main()

        Dim jro As JRO.JetEngine

        jro = New JRO.JetEngine()

        jro.CompactDatabase("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/nwind.mdb", _
        "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/NewNwind.mdb;Jet OLEDB:Engine Type=5")

        MsgBox("Finished Compacting Database!")
    End Sub

End Module
					
8.Change the path to the Source and Destination .mdb files as appropriate. Press F5 to build and run the project.

The compacted database will be in Access 2000 (Jet 4.0) format. For a different Jet format, see "References."

Pitfalls

In order to compact a database, the Jet Database Engine requires exclusive access to the database file. Attempting to compact a database file that is currently in use will result in an exception. This exception can be caught using a Try...Catch structure.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值