【发布时间】:2009-12-08 11:39:44
【问题描述】:
如果可以的话,是否可以使用 c# 压缩 Msaccess 数据库?
【问题讨论】:
如果可以的话,是否可以使用 c# 压缩 Msaccess 数据库?
【问题讨论】:
你可以试试这样的
public static void CompactAndRepair(string accessFile, Microsoft.Office.Interop.Access.Application app)
{
string tempFile = Path.Combine(Path.GetDirectoryName(accessFile),
Path.GetRandomFileName() + Path.GetExtension(accessFile));
app.CompactRepair(accessFile, tempFile, false);
app.Visible = false;
FileInfo temp = new FileInfo(tempFile);
temp.CopyTo(accessFile, true);
temp.Delete();
}
另见Use the CompactRepair method of the Application object to compact and repair the database
【讨论】:
...
//invoke a CompactDatabase method of a JRO object
//pass Parameters array
objJRO.GetType().InvokeMember("CompactDatabase",
System.Reflection.BindingFlags.InvokeMethod,
null,
objJRO,
oParams);
...
在http://www.codeproject.com/KB/database/mdbcompact_latebind.aspx查看更多详情
【讨论】:
我使用了这篇文章:
Compact and Repair an Access Database Programmatically Using C#
我对其进行了一些修改以使其更易于使用:
public static bool CompactAndRepairAccessDB(string source, string destination)
{
try
{
JetEngine engine = (JetEngine)HttpContext.Current.Server.CreateObject("JRO.JetEngine");
engine.CompactDatabase(string.Format("Data Source={0};Provider=Microsoft.Jet.OLEDB.4.0;", source),
string.Format("Data Source={0};Provider=Microsoft.Jet.OLEDB.4.0;", destination));
File.Copy(destination, source, true);
File.Delete(destination);
return true;
}
catch
{
return false;
}
}
别忘了:
在解决方案资源管理器中右键单击引用 -> 添加引用 -> COM -> 搜索“jet” -> 添加“Microsoft Jet 和复制对象...”
并添加:
using System.IO;
using JRO;
【讨论】: