This code compacts and repairs an MS Access database from a C# .NET application-阿里云开发者社区

开发者社区> 老朱教授> 正文

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.gifContractedBlock.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.gifpublic static void CompactAccessDB(string connectionString, string mdwfilename)
ExpandedBlockStart.gifContractedBlock.gifdot.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,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
阿里云服务器怎么设置密码?怎么停机?怎么重启服务器?
如果在创建实例时没有设置密码,或者密码丢失,您可以在控制台上重新设置实例的登录密码。本文仅描述如何在 ECS 管理控制台上修改实例登录密码。
7840 0
阿里云服务器端口号设置
阿里云服务器初级使用者可能面临的问题之一. 使用tomcat或者其他服务器软件设置端口号后,比如 一些不是默认的, mysql的 3306, mssql的1433,有时候打不开网页, 原因是没有在ecs安全组去设置这个端口号. 解决: 点击ecs下网络和安全下的安全组 在弹出的安全组中,如果没有就新建安全组,然后点击配置规则 最后如上图点击添加...或快速创建.   have fun!  将编程看作是一门艺术,而不单单是个技术。
9564 0
使用SSH远程登录阿里云ECS服务器
远程连接服务器以及配置环境
2167 0
阿里云服务器如何登录?阿里云服务器的三种登录方法
购买阿里云ECS云服务器后如何登录?场景不同,阿里云优惠总结大概有三种登录方式: 登录到ECS云服务器控制台 在ECS云服务器控制台用户可以更改密码、更换系.
11223 0
腾讯云服务器 设置ngxin + fastdfs +tomcat 开机自启动
在tomcat中新建一个可以启动的 .sh 脚本文件 /usr/local/tomcat7/bin/ export JAVA_HOME=/usr/local/java/jdk7 export PATH=$JAVA_HOME/bin/:$PATH export CLASSPATH=.
4499 0
阿里云服务器ECS登录用户名是什么?系统不同默认账号也不同
阿里云服务器Windows系统默认用户名administrator,Linux镜像服务器用户名root
3010 0
阿里云ECS云服务器初始化设置教程方法
阿里云ECS云服务器初始化是指将云服务器系统恢复到最初状态的过程,阿里云的服务器初始化是通过更换系统盘来实现的,是免费的,阿里云百科网分享服务器初始化教程: 服务器初始化教程方法 本文的服务器初始化是指将ECS云服务器系统恢复到最初状态,服务器中的数据也会被清空,所以初始化之前一定要先备份好。
6432 0
+关注
3546
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载