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,如需转载请自行联系原作者

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

相关文章
怎么设置阿里云服务器安全组?阿里云安全组规则详细解说
阿里云服务器安全组设置规则分享,阿里云服务器安全组如何放行端口设置教程
6915 0
阿里云服务器ECS远程登录用户名密码查询方法
阿里云服务器ECS远程连接登录输入用户名和密码,阿里云没有默认密码,如果购买时没设置需要先重置实例密码,Windows用户名是administrator,Linux账号是root,阿小云来详细说下阿里云服务器远程登录连接用户名和密码查询方法
2852 0
阿里云服务器端口号设置
阿里云服务器初级使用者可能面临的问题之一. 使用tomcat或者其他服务器软件设置端口号后,比如 一些不是默认的, mysql的 3306, mssql的1433,有时候打不开网页, 原因是没有在ecs安全组去设置这个端口号. 解决: 点击ecs下网络和安全下的安全组 在弹出的安全组中,如果没有就新建安全组,然后点击配置规则 最后如上图点击添加...或快速创建.   have fun!  将编程看作是一门艺术,而不单单是个技术。
4485 0
使用OpenApi弹性释放和设置云服务器ECS释放
云服务器ECS的一个重要特性就是按需创建资源。您可以在业务高峰期按需弹性的自定义规则进行资源创建,在完成业务计算的时候释放资源。本篇将提供几个Tips帮助您更加容易和自动化的完成云服务器的释放和弹性设置。
7758 0
windows server 2008阿里云ECS服务器安全设置
最近我们Sinesafe安全公司在为客户使用阿里云ecs服务器做安全的过程中,发现服务器基础安全性都没有做。为了为站长们提供更加有效的安全基础解决方案,我们Sinesafe将对阿里云服务器win2008 系统进行基础安全部署实战过程! 比较重要的几部分 1.
5458 0
阿里云服务器安全组设置内网互通的方法
虽然0.0.0.0/0使用非常方便,但是发现很多同学使用它来做内网互通,这是有安全风险的,实例有可能会在经典网络被内网IP访问到。下面介绍一下四种安全的内网互联设置方法。 购买前请先:领取阿里云幸运券,有很多优惠,可到下文中领取。
9426 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=.
2141 0
+关注
3545
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
文娱运维技术
立即下载
《SaaS模式云原生数据仓库应用场景实践》
立即下载
《看见新力量:二》电子书
立即下载