是否可以使用c#压缩Ms Access数据库?如果是这样,让我知道方法吗?
答案 0 :(得分:5)
...
//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
上查看更多详情答案 1 :(得分:4)
您可以尝试这样的事情
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
答案 2 :(得分:1)
我用过这篇文章:
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;