第一种(未测试过,比较麻烦不是?):
添加引用COM:C:\winnt\system32\msjro.dll
调用:using MyJro;//名称空间
void CompressAccess(){ string StrConn1="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("/bin/ip.mdb");
string StrConn2="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("/bin/ip2.mdb");
JetEngine DatabaseEngin=new JetEngine(); DatabaseEngin.CompactDatabase(StrConn1,StrConn2); } //ip2.mdb是压缩后的数据库,压缩后,如果恢复备份删除ip.mdb,
第二种:
//原数据库
private string dataBasePath;
//备份后的数据库名
private string tempDataBasePath=HttpContext.Current.Request.PhysicalApplicationPath+"/data/tempdb.mdb";
//数据库连接字符串
private string connectionString;
//..省略代码
public void CompactAccessDB()
{
if (System.IO.File.Exists(tempDataBasePath))
{
System.IO.File.Delete(tempDataBasePath);
}
else
{
object[] oParams;
object objJRO = Activator.CreateInstance(Type.GetTypeFromProgID("JRO.JetEngine"));
oParams = new object[] { connectionString, "Provider=Microsoft.Jet.OLEDB.4.0;Data" + " Source=" + tempDataBasePath + ";Jet OLEDB:Engine Type=5" };
objJRO.GetType().InvokeMember("CompactDatabase", System.Reflection.BindingFlags.InvokeMethod, null, objJRO, oParams);
System.Runtime.InteropServices.Marshal.ReleaseComObject(objJRO);
objJRO = null;
}
}
private string dataBasePath;
//备份后的数据库名
private string tempDataBasePath=HttpContext.Current.Request.PhysicalApplicationPath+"/data/tempdb.mdb";
//数据库连接字符串
private string connectionString;
//..省略代码
public void CompactAccessDB()
{
if (System.IO.File.Exists(tempDataBasePath))
{
System.IO.File.Delete(tempDataBasePath);
}
else
{
object[] oParams;
object objJRO = Activator.CreateInstance(Type.GetTypeFromProgID("JRO.JetEngine"));
oParams = new object[] { connectionString, "Provider=Microsoft.Jet.OLEDB.4.0;Data" + " Source=" + tempDataBasePath + ";Jet OLEDB:Engine Type=5" };
objJRO.GetType().InvokeMember("CompactDatabase", System.Reflection.BindingFlags.InvokeMethod, null, objJRO, oParams);
System.Runtime.InteropServices.Marshal.ReleaseComObject(objJRO);
objJRO = null;
}
}
同样,恢复备份就是先删除原数据库.在线压缩还原得先关掉站点.
//用备份的库替换原数据库
System.IO.File.Move(tempDataBasePath,dataBasePath);
我用的是第二种.希望对你有用