需要在工程中引用COM组件: Microsoft Jet and Replication Objects Library ,示例请参考下面的函数:
public static bool CompactJetDatabasestring fileName
{
// I use this function as part of an AJAX page, so rather than throwing
// exceptions if errors are encountered, I simply return false and allow the page
// to handle the failure generically.
try
{
if fileName.Equals''
return false;
string oldFileName = fileName;
// 创建一个生成后的临时文件
string newFileName = Path.CombinePath.GetDirectoryNameoldFileName, Guid.NewGuid.ToString'N' + '.mdb';
// 创建压缩类
JetEngineClass engine = new JetEngineClass;
// 压缩MDB为新的文件
engine.CompactDatabase
String.FormatAccessOleDbConnectionStringFormat, oldFileName,
String.FormatAccessOleDbConnectionStringFormat, newFileName;
// 删除旧文件
File.DeleteoldFileName;
// 改名为旧文件名.
File.MovenewFileName, oldFileName;
return true;
}
catch Exception ex
{
return false;
}
}
v