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

推荐订阅源

V
Vulnerabilities – Threatpost
F
Fortinet All Blogs
Vercel News
Vercel News
C
Check Point Blog
P
Privacy International News Feed
Know Your Adversary
Know Your Adversary
Google DeepMind News
Google DeepMind News
T
Troy Hunt's Blog
TaoSecurity Blog
TaoSecurity Blog
I
Intezer
T
The Exploit Database - CXSecurity.com
Security Archives - TechRepublic
Security Archives - TechRepublic
H
Hacker News: Front Page
P
Proofpoint News Feed
GbyAI
GbyAI
Engineering at Meta
Engineering at Meta
Attack and Defense Labs
Attack and Defense Labs
S
Security @ Cisco Blogs
IT之家
IT之家
D
DataBreaches.Net
Hacker News: Ask HN
Hacker News: Ask HN
SecWiki News
SecWiki News
Y
Y Combinator Blog
Project Zero
Project Zero
H
Hackread – Cybersecurity News, Data Breaches, AI and More
L
Lohrmann on Cybersecurity
T
Tenable Blog
大猫的无限游戏
大猫的无限游戏
L
LINUX DO - 最新话题
G
Google Developers Blog
The GitHub Blog
The GitHub Blog
Recorded Future
Recorded Future
有赞技术团队
有赞技术团队
Martin Fowler
Martin Fowler
K
Kaspersky official blog
PCI Perspectives
PCI Perspectives
A
Arctic Wolf
Latest news
Latest news
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
Netflix TechBlog - Medium
雷峰网
雷峰网
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
P
Palo Alto Networks Blog
The Hacker News
The Hacker News
WordPress大学
WordPress大学
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
月光博客
月光博客
Schneier on Security
Schneier on Security
M
MIT News - Artificial intelligence

博客园 - 紫雨轩 .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)  评论()    收藏  举报