private void Button_Click_1(object sender, RoutedEventArgs e) { if (isRecovering) { MessageBox.Show("有数据库正在还原,请等待还原完成后再执行删除操作!"); return; } //删除选中数据库 if (DataGrid1.SelectedItem == null) { MessageBox.Show("未选中任何数据库", "提示"); } else { DBBackupPath fileT = DataGrid1.SelectedItem as DBBackupPath; string path = fileT.Path; string tips = string.Format("即将删除备份数据库文件,文件路径:{0}", path); if (MessageBox.Show(tips, "提示", MessageBoxButton.OK) == MessageBoxResult.OK) { if (File.Exists(path)) { File.Delete(path); InitRecoverGrid(); } else { MessageBox.Show("删除失败,路径下文件不存在!"); } } } }
public static DBBackupPath[] GetPathes(string[] files) { if (files == null) { return(null); } List <DBBackupPath> pathes = new List <DBBackupPath>(); foreach (var item in files) { DBBackupPath back = new DBBackupPath(); back.Path = item; pathes.Add(back); } return(pathes.ToArray()); }
private void InitRecoverGrid() { if (string.IsNullOrEmpty(dbSavePath)) { dbSavePath = AppDomain.CurrentDomain.BaseDirectory + "\\Data\\SqlBackup\\"; } string[] files = Directory.GetFiles(dbSavePath, "*.sql"); if (files == null || files.Length == 0) { return; } DBBackupPath[] pathes = DBBackupPath.GetPathes(files); if (pathes != null) { DataGrid1.ItemsSource = pathes; } }
private bool isRecovering;//是否正在还原数据库 private void Button_Click(object sender, RoutedEventArgs e) { if (isRecovering) { MessageBox.Show("数据库还原中,请耐心等待..."); return; } if (DataGrid1.SelectedItem == null) { MessageBox.Show("未选中任何数据库", "提示"); } else { isRecovering = true; DBBackupPath fileT = DataGrid1.SelectedItem as DBBackupPath; string path = fileT.Path; DateTime timeNow = DateTime.Now; bool isRecoverSuccess = true;//线程内出现异常后,执行异常回调后,又会执行结束回调。增加这个标志位区分是否成功 RecoverInfoText.Text = "数据库正在还原中,请耐心等待。请勿关闭当前页面!"; Worker.Run(() => { string[] pathSplit = path.Split('\\'); string dbTargetName = LocationMySql;//需要还原的数据库名称 if (pathSplit != null && pathSplit.Length > 0) { string finalName = pathSplit[pathSplit.Length - 1].ToLower(); if (finalName.Contains(LocationHistoryMySql.ToLower())) { dbTargetName = LocationHistoryMySql; } } else { if (path.Contains(LocationHistoryMySql.ToLower())) { dbTargetName = LocationHistoryMySql; } } string tips = string.Format("文件路径:{0} 目标数据库名称:{1}\n点击确认开始还原,点击取消则退出。", path, dbTargetName); if (MessageBox.Show(tips, "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK) { RecoverDBByFullPath(path, dbTargetName); } else { isRecoverSuccess = false; } }, () => { isRecovering = false; if (isRecoverSuccess) { string costTime = (DateTime.Now - timeNow).TotalSeconds.ToString(); string successInfo = string.Format("数据库还原成功,耗时:{0} 秒", costTime); RecoverInfoText.Text = successInfo; MessageBox.Show(successInfo); } else { RecoverInfoText.Text = "数据库还原已取消..."; } }, exp => { isRecoverSuccess = false; isRecovering = false; string errorInfo = string.Format("Error:数据库还原失败,错误原因:{0}", exp.ToString()); MessageBox.Show(errorInfo); RecoverInfoText.Text = errorInfo; }); } }