⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 backup.cs

📁 数据库的备份
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.IO;

namespace DbBackUp
{
    /// <summary>
    /// 备份、恢复数据库文件
    /// </summary>
    public class BackUp
    {
        public BackUp(string db,string log,string backDB,string backLog)
        {
            this.pathDB = db;
            this.pathLog = log;
            this.pathBackUpDB = backDB;
            this.pathBackUpLog = backLog;
        }

        private string pathDB;
        private string pathLog;
        private string pathBackUpDB;
        private string pathBackUpLog;

        /// <summary>
        /// 备份数据库文件
        /// </summary>
        /// <returns></returns>
        public FileInfo[] BackUpData()
        {
            try
            {
                FileInfo dbInfo = new FileInfo(this.pathDB);
                FileInfo logInfo = new FileInfo(this.pathLog);

                dbInfo.CopyTo(this.pathBackUpDB, true);
                logInfo.CopyTo(this.pathBackUpLog, true);
                if (!File.Exists(this.pathBackUpDB) && !File.Exists(this.pathBackUpLog))
                {
                    return null;
                }

                FileInfo backDb = new FileInfo(this.pathBackUpDB);
                FileInfo backLog = new FileInfo(this.pathBackUpLog);
             
                FileInfo[] infors = new FileInfo[] { dbInfo, logInfo, backDb, backLog };
                return infors;
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据库备份失败!" + ex.Message, "错误提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }


        }

        /// <summary>
        /// 还原数据库文件
        /// </summary>
        public FileInfo[] ReductionData()
        {
            try
            { 
                FileInfo backDb = new FileInfo(this.pathBackUpDB);
                FileInfo backLog = new FileInfo(this.pathBackUpLog);
                if (!File.Exists(this.pathBackUpDB) && !File.Exists(this.pathBackUpLog))
                {
                    return null;
                }

                string confirm = "是否将数据库还原到:" + backDb.LastWriteTime.ToLongDateString() + " " + backDb.LastWriteTime.ToLongTimeString();
                if (MessageBox.Show(confirm,"确认还原",MessageBoxButtons.OKCancel,MessageBoxIcon.Question) != DialogResult.OK)
                {
                    return null;
                }
               
                backDb.CopyTo(this.pathDB, true);
                backLog.CopyTo(this.pathLog, true);

                FileInfo dbInfo = new FileInfo(this.pathDB);
                FileInfo logInfo = new FileInfo(this.pathLog);

                FileInfo[] infors = new FileInfo[] { dbInfo, logInfo, backDb, backLog };
                return infors;
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据库还原失败!" + ex.Message, "错误提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -