This code compacts and repairs an MS Access database from a C# .NET application

简介:

Introduction
This code compacts and repairs an MS Access database from a C# .NET application, no matter if it's a simple ".mdb", or a ".mdw"-secured workgroup-shared DB. It performs exactly the same operation as "Tools - Database Utils - Compact and Repair Database..." menu item in the MS Access application. The code uses "late binding" (creating COM-objects in memory at runtime), and that's why you don't need any annoying interop COM references in your project. You don't even need MS Office installed. Just make sure you have a Jet Engine (Jet is included in MDAC package, which comes with any Windows installation starting from NT 4).

Background
Don't you hate COM-library references in .NET-projects? I believe that pure .NET-code has to be free of any interops, RCWs, and other referenced COM-stuff. Basically because there's a load of different versions of MS libraries (for example, MS Office Object Library 9, 10, 11 etc.). We never know what version of MS Office is installed on a client machine, that's why we should access a COM-object via ProgID, and not CLSID. For example: you want to be sure, that when you call for an "Excel.Application", you get Excel, no matter what version of MS Office is installed. And when you add a reference "MS Excel 10 Object library", you add a strong limitation to your software! So... use System.Reflection and late binding.

Using the code
Just call a method CompactAccessDB. This method compacts and repairs your database.

Parameters:

connectionString - connection string to your database.
mdwfilename - is a full name (path+name) of an MDB-file you want to compact and repair.
Due to Jet limitations, the method compacts your database to a new file, so we have to copy the new compacted file over an old one.

When you call this method, make sure that there's no open connections to your database. Stop your threads.

Now, to the code:

ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif
/// MBD compact method (c) 2004 Alexander Youmashev
InBlock.gif
/// !!IMPORTANT!!
InBlock.gif
/// !make sure there's no open connections
InBlock.gif
/// to your db before calling this method!
InBlock.gif
/// !!IMPORTANT!!
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="connectionString">connection string to your db</param>
InBlock.gif
/// <param name="mdwfilename">FULL name
ExpandedBlockEnd.gif
/// of an MDB file you want to compress.</param>

None.gif public static void CompactAccessDB( string connectionString, string mdwfilename)
ExpandedBlockStart.gif ContractedBlock.gif dot.gif {
InBlock.gifobject[] oParams;
InBlock.gif
InBlock.gif//create an inctance of a Jet Replication Object
InBlock.gif
object objJRO =
InBlock.gif Activator.CreateInstance(Type.GetTypeFromProgID("JRO.JetEngine"));
InBlock.gif
InBlock.gif//filling Parameters array
InBlock.gif
//cnahge "Jet OLEDB:Engine Type=5" to an appropriate value
InBlock.gif
// or leave it as is if you db is JET4X format (access 2000,2002)
InBlock.gif
//(yes, jetengine5 is for JET4X, no misprint here)
ExpandedSubBlockStart.gifContractedSubBlock.gif
oParams = new object[] dot.gif{
InBlock.gif connectionString,
InBlock.gif "Provider=Microsoft.Jet.OLEDB.4.0;Data" +
ExpandedSubBlockEnd.gif " Source=C:\\tempdb.mdb;Jet OLEDB:Engine Type=5"}
;
InBlock.gif
InBlock.gif//invoke a CompactDatabase method of a JRO object
InBlock.gif
//pass Parameters array
InBlock.gif
objJRO.GetType().InvokeMember("CompactDatabase",
InBlock.gif System.Reflection.BindingFlags.InvokeMethod,
InBlock.gifnull,
InBlock.gif objJRO,
InBlock.gif oParams);
InBlock.gif
InBlock.gif//database is compacted now
InBlock.gif
//to a new file C:\\tempdb.mdw
InBlock.gif
//let's copy it over an old one and delete it
InBlock.gif

InBlock.gif System.IO.File.Delete(mdwfilename);
InBlock.gif System.IO.File.Move("C:\\tempdb.mdb", mdwfilename);
InBlock.gif
InBlock.gif//clean up (just in case)
InBlock.gif
System.Runtime.InteropServices.Marshal.ReleaseComObject(objJRO);
InBlock.gif objJRO=null;
ExpandedBlockEnd.gif}

None.gif


Points of Interest
Interesting, that Jet Engine 5 is used for JET4X databases. Be careful. See the table:

Jet OLEDB:Engine Type Jet x.x Format MDB Files
1 JET10
2 JET11
3 JET2X
4 JET3X
5 JET4X




本文转自suifei博客园博客,原文链接:http://www.cnblogs.com/Chinasf/archive/2005/04/17/139208.html,如需转载请自行联系原作者

相关文章
|
3月前
|
测试技术
Could not proxy command to the remote server. Original error: timeout of 240000ms exceeded 的解决办法
Could not proxy command to the remote server. Original error: timeout of 240000ms exceeded 的解决办法
125 0
|
SQL 安全 数据库
MS SQL Could not obtain information about Windows NT group/user &#39;domain\login&#39;, error code 0x5. [SQLSTATE 42000] (Error 15404)
最近碰到一个有趣的错误:海外的一台数据库服务器上某些作业偶尔会报错,报错信息如下所示: ---------------------------------------------------------------------------------------------------------...
1218 0
|
SQL 数据库 Windows
SQL Server 2005 sp_send_dbmail出现Internal error at FormatRowset (Reason: Not enough storage is available to complete this operation)
案例环境:   操作系统: Windows 2003 SE 32bit(SP2) 数据库版本:Microsoft SQL Server 2005 - 9.00.5069.00 (Intel X86)             Aug 22 2012 16:01:52           ...
1392 0
|
SQL 安全 测试技术
MS SQL 错误:The operation could not be performed because OLE DB provider "SQLNCLI10" for linked server "test" was unable to begin a distributed transact
一同事在测试服务器(系统:Windows 2008 R2 Standard 数据库:SQL SERVER 2008 R2)通过链接服务器test使用分布式事务测试时出错,出错信息如下: set xact_abort on begin tran update test.
1415 0
|
SQL 安全 数据库
The server principal "sa" is not able to access the database "xxxx" under the current security context
在SQL Server服务器上一个作业执行时,遇到下面错误信息: Message: Executed as user: dbo. The server principal "sa" is not able to access the database "xxxx" under the current security context. [SQLSTATE 08004] (Error 916). The step failed.   作业本身执行的存储过程非常简单,就是将数据库A中的历史数据处理过后,归档到A_History库中,结果就遇到这么一个问题。
2853 0
|
SQL Windows
Unable to determine if the owner (Domain\UserName) of job JOB_NAME has server access
早上巡检的的时候,发现一数据库的作业报如下错误(作业名等敏感信息已经替换),该作业的OWNER为一个域账号: JOB RUN: 'JOB_NAME' was run on 2016-6-1 at 7:00:00 DURATION: 0 hours, 0 minutes, 1 seconds STATUS: Failed MESSAGES: The job failed.
1400 0
|
关系型数据库 Oracle
Using deprecated SQLNET.ALLOWED_LOGON_VERSION parameter
Using deprecated SQLNET.ALLOWED_LOGON_VERSION parameter.
5042 0
|
SQL 关系型数据库 HIVE
MetaException(message:For direct MetaStore DB connections, we don't support retries at the client level.)
在mysql中执行以下命令:   drop database hive;   create database hive;   alter database hive character set latin1;   重启hive
2837 0