/// <summary>
/// 備份Access數據庫
/// </summary>
/// <param name="srcPath">要備份的數據庫絕對路徑</param>
/// <param name="aimPath">備份到的數據庫絕對路徑</param>
/// <returns></returns>
public bool Backup(string srcPath, string aimPath)
{
if (!File.Exists(srcPath))
{
throw new Exception("源數據庫不存在,無法備份");
}
try
{
File.Copy(srcPath, aimPath, true);
}
catch (IOException ixp)
{
return false;
throw new Exception(ixp.ToString());
}
return true;
}
/// <summary>
/// 還原Access數據庫
/// </summary>
/// <param name="bakPath">備份的數據庫絕對路徑</param>
/// <param name="dbPath">要還原的數據庫絕對路徑</param>
public bool RecoverAccess(string bakPath, string dbPath)
{
if (!File.Exists(bakPath))
{
throw new Exception("備份數據庫不存在,無法還原");
}
try
{
File.Copy(bakPath, dbPath, true);
}
catch (IOException ixp)
{
return false;
throw new Exception(ixp.ToString());
}
return true;
}
//備份的事件及獲取備份路徑和還原的路徑
protected void Button1_Click(object sender, EventArgs e)
{
string path = HttpContext.Current.Server.MapPath("../App_Data/df.mdb");
string pathdata = path ;
string pathback = path.Substring(0, path.Length - 15) + @"dataBack\df.mdb";
if (Backup(pathdata, pathback))
{
Label3.Text = "備份成功!";
}
else
{
Label3.Text = "備份失敗!";
}
}
//還原的事件及獲取備份路徑和還原的路徑
protected void Button2_Click(object sender, EventArgs e)
{
string path = HttpContext.Current.Server.MapPath("../App_Data/df.mdb");
string pathdata = path;
string pathback = path.Substring(0, path.Length - 15) + @"dataBack\df.mdb";
if (RecoverAccess(pathback, pathdata))
{
Label3.Text = "還原成功!";
}
else
{
Label3.Text = "還原失敗!";
}
}