惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

博客园 - 紫雨轩 .Net

DNGuard Enterprise v3.56 released DNGuard Enterprise v3.30 released ComboBox 使用数据绑定时 Sorted 属性的bug DNGuard Enterprise v3.2 released DNGuard 企业版 v3.1 发布 DNGuard 专业版 v2.95 发布 DNGuard Enterprise v2.95 released 在 FlexGrid 控件中指定最右侧显示的列 .Net 中枚举AppDomains Windows 2003 上使用 Windows Live Writer .Net程序集的不同加载方式,以及其在内存中格式 DNGuard Enterprise v2.92 released - 紫雨轩 .Net DNGuard 标准版 v2.90发布 DNGuard Enterprise v2.90 released DNGuard Enterprise v2.82 released 采用Native 引导方式的.Net加密保护 直接在.Net程序(C#)中执行 native code C#复杂表达式的问题 DNGuard HVM Trial V2.82 发布
C#中使用晚绑定实现压缩Access数据库
紫雨轩 .Net · 2008-01-25 · via 博客园 - 紫雨轩 .Net

C#中使用晚绑定实现压缩Access数据库.
VB对Com后期绑定支持得很好,在C#中可以使用反射来实现.

函数实现代码如下:

 1 public static void CompactAccessDB(string strMdbName)
 2 {
 3     string TempMdbName = Application.StartupPath + @"\Temp.mdb";
 4 
 5     //创建 Jet 引擎对象
 6     object objJetEngine = Activator.CreateInstance(Type.GetTypeFromProgID("JRO.JetEngine"));
 7 
 8     //设置参数数组
 9     //根据你所使用的Access版本修改 "Jet OLEDB:Engine Type=5" 中的数字.
10     //5 对应 JET4X 格式 (access 2000,2002)
11 
12     object[] objParams = new object[] {
13         String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}",strMdbName), //输入连接字符串
14         String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Jet OLEDB:Engine Type=5",TempMdbName) //输出连接字符串
15         };
16 
17     //通过反射调用 CompactDatabase 方法
18     objJetEngine.GetType().InvokeMember("CompactDatabase",
19             System.Reflection.BindingFlags.InvokeMethod,
20             null,
21             objJetEngine,
22             objParams);
23 
24     //删除原数据库文件
25     System.IO.File.Delete(strMdbName);
26     //重命名压缩后的数据库文件
27     System.IO.File.Move(TempMdbName, strMdbName);
28     //释放Com组件
29     System.Runtime.InteropServices.Marshal.ReleaseComObject(objJetEngine);
30     objJetEngine = null;
31 }


为什么要使用晚绑定呢?可以直接在项目中添加对 JRO.JetEngine 组件的引用来实现的.

在多人协作的开发环境中,添加引用需要签出项目文件进行修改.如果有组员机器上没有这个组件,她就会无法编译改动后程序.

这种方式就很方便,复制过去就能用.不需要对项目进行修改.

虽然晚绑定有小小性能损失,换来的方便性还是划算的. 而且也不是经常会用到这个功能.

对于有密码的access文件怎么处理呢?
很简单的,就是在输入连接字符串中增加密码的设置即可.

这样默认压缩后的mdb就没有密码,如果希望压缩后的mdb也有密码,就在输出的连接字符串里面增加密码设置即可.

这个方法其实也可以用来修改mdb的密码,取消mdb密码,给mdb设置密码.

posted on 2008-01-25 14:20  紫雨轩 .Net  阅读(2771)  评论()    收藏  举报