/// <summary> /// 数据库完整还原按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void fullRestoreDatabase_Click(object sender, EventArgs e)//完整还原按钮 { using (OpenFileDialog fileDialog = new OpenFileDialog()) { fileDialog.Filter = "(*.bak)|*.bak"; if (fileDialog.ShowDialog() == DialogResult.OK) { //获取用户选择文件的后缀名 //string extension = Path.GetExtension(fileDialog.FileName); //获取用户选择文件的文件名 string fileName = Path.GetFileNameWithoutExtension(fileDialog.FileName); if (fileName.Contains("Full")) { string[] strList = new string[] { fileDialog.FileName }; BackupAndRestored.FullRestoreDatabase(strList, this.UserInfo, listLog2, BacOrResType.Full); } else { MessageBox.Show("请选择完整的备份文件(文件名中包含“Full”字样)"); listLog2.AddToButtom("数据库还原失败...文件格式错误"); listLog2.AddToButtom("请使用“完整还原”按钮重新选择备份文件"); } } } }
//数据库还原逻辑,仅被一键还原调用. public void restoredDataBase() { #region 数据库还原逻辑 //todo:是否添加跳过数据库还原对话框. using (FolderBrowserDialog dialog = new FolderBrowserDialog()) { string sqlBackupFileForderPath = ConfigHelper.GetAppConfig("sqlBackupFileForderPath"); if (string.IsNullOrEmpty(sqlBackupFileForderPath)) { sqlBackupFileForderPath = this.UserInfo.AppPath + "\\SQLBackup"; } DirectoryInfo TheFolder = new DirectoryInfo(sqlBackupFileForderPath); if (!TheFolder.Exists || TheFolder.GetFiles("*.bak").Length == 0) { dialog.ShowNewFolderButton = true; dialog.Description = "数据库备份文件所在的文件夹"; //dialog.RootFolder = Environment.SpecialFolder.Recent; if (dialog.ShowDialog(this) != DialogResult.OK) { listLog2.AddToButtom("未能选择文件夹,还原终止..."); return; } TheFolder = new DirectoryInfo(dialog.SelectedPath); } FileInfo[] fullFileInfo = TheFolder.GetFiles("backupFull.bak"); FileInfo[] defFileInfo = TheFolder.GetFiles("backupDef.bak"); if (defFileInfo.Length > 0) { //差异还原 if (fullFileInfo.Length == 0) { listLog2.AddToButtom("找不到备份文件,还原终止..."); return; } string[] backFiles = new string[] { TheFolder.FullName + "\\" + fullFileInfo[0].Name, TheFolder.FullName + "\\" + defFileInfo[0].Name }; BackupAndRestored.FullRestoreDatabase(backFiles, UserInfo, listLog2, BacOrResType.Def); } else { //完整还原 //检查文件个数 if (fullFileInfo.Length == 0) { listLog2.AddToButtom("找不到备份文件,还原终止..."); return; } string[] backFiles = new string[] { TheFolder.FullName + "\\" + fullFileInfo[0].Name }; BackupAndRestored.FullRestoreDatabase(backFiles, UserInfo, listLog2, BacOrResType.Full); } } #endregion }
/// <summary> /// 数据库差异还原按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void skinButton1_Click(object sender, EventArgs e) { using (OpenFileDialog fileDialog = new OpenFileDialog()) { fileDialog.Filter = "(*.bak)|*.bak"; fileDialog.Multiselect = true; fileDialog.Title = "同时选择完整备份和差异备份文件"; if (fileDialog.ShowDialog() == DialogResult.OK) { if (fileDialog.FileNames.Length != 2) { listLog2.AddToButtom("需同时选中完整备份和差异备份两个文件..."); listLog2.AddToButtom("还原终止...False"); return; } //int fullFile = 0; //int defFile = 0; string[] strList = new string[2]; foreach (var item in fileDialog.FileNames) { if (Path.GetFileNameWithoutExtension(item).Contains("Full")) { strList[0] = item; //fullFile += 1; } else if (Path.GetFileNameWithoutExtension(item).Contains("Def")) { strList[1] = item; //defFile += 1; } } if (string.IsNullOrEmpty(strList[0]) || string.IsNullOrEmpty(strList[1])) { listLog2.AddToButtom("所选文件名称不正确(需包含\"full\"或\"def\")"); listLog2.AddToButtom("还原终止...False"); return; } BackupAndRestored.FullRestoreDatabase(strList, this.UserInfo, listLog2, BacOrResType.Def); } } }