C#實現ACCESS數據庫備份還原


/// <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 = "還原失敗!";
        }
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM