備份,還原,壓縮資料庫
來源:互聯網
上載者:User
壓縮Access資料庫#region 壓縮Access資料庫
/**//// <summary>
/// 壓縮Access資料庫
/// </summary>
/// <param name=&qu
public static void CompactAccess(string DBPath)
...{
if (!File.Exists(DBPath))
...{
throw new Exception("目標資料庫不存在,無法壓縮");
}
//聲明臨時資料庫名稱
string temp = DateTime.Now.Year.ToString();
temp += DateTime.Now.Month.ToString();
temp += DateTime.Now.Day.ToString();
temp += DateTime.Now.Hour.ToString();
temp += DateTime.Now.Minute.ToString();
temp += DateTime.Now.Second.ToString() + ".bak";
temp = DBPath.Substring(0, DBPath.LastIndexOf("\") + 1) + temp;
//定義臨時資料庫的連接字串
string temp2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+temp;
//定義目標資料庫的連接字串
string DBPath2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+DBPath;
//建立一個JetEngineClass對象的執行個體
JRO
//使用JetEngineClass對象的CompactDatabase方法壓縮修複資料庫
jt.CompactDatabase(DBPath2, temp2);
//拷貝臨時資料庫到目標資料庫(覆蓋)
File.Copy(temp, DBPath, true);
//最後刪除臨時資料庫
File.Delete(temp);
}