public bool BackupDatabase(bool closeProgressWindowOnCompleted) { if (!Directory.Exists(BackupLocation)) { InteractionService.ShowError(string.Format(UiResources.FolderNotExist_1, BackupLocation), null); return(false); } if (!IsFileNameValid(BackupFileName)) { InteractionService.ShowError(UiResources.InvalidFileName, null); return(false); } string filePath = Path.Combine(BackupLocation, BackupFileName); if (File.Exists(filePath)) { bool overwrite = false; InteractionService.ShowConfirmation(UiResources.FileAlreadyExist, (confirmed) => { overwrite = confirmed; }); if (!overwrite) { return(false); } } var progressViewModel = new ProgressViewModel(); progressViewModel.Title = UiResources.Backup; progressViewModel.Message = string.Format(UiResources.BackupToFile_1, filePath); bool backupResult = true; InteractionService.ShowProgress(progressViewModel, (sender, e) => { var dbManager = new DbManager(); dbManager.BackupPercentComplete += progressViewModel.OnPercentChanged; dbManager.BackupComplete += progressViewModel.OnCompleted; new RadBusyModel().DoWorkAsync(() => { try { dbManager.BackupDatabase(MyDbUtility.GetConnection(), MyDbUtility.GetDatabaseName(), filePath); } catch (Exception ex) { backupResult = false; progressViewModel.IsFinished = true; progressViewModel.ValidationResults.Add(new ValidationResult(ex.MostInnerException().Message)); } }); }, closeProgressWindowOnCompleted); return(backupResult); }
public bool RestoreDatabase(bool closeProgressWindowOnCompleted) { if (!File.Exists(RestoreFilePath)) { InteractionService.ShowError(string.Format(UiResources.FileNotExist_1, RestoreFilePath), null); return(false); } bool confirm = false; InteractionService.ShowConfirmation(string.Format(UiResources.RestoreConfirm_1, RestoreFilePath), (confirmed) => { confirm = confirmed; }); if (!confirm) { return(false); } var progressViewModel = new ProgressViewModel(); progressViewModel.Title = UiResources.Restore; progressViewModel.Message = string.Format(UiResources.RestoreFromFile_1, RestoreFilePath); bool restoreResult = true; InteractionService.ShowProgress(progressViewModel, (sender, e) => { var dbManager = new DbManager(); dbManager.RestorePercentComplete += progressViewModel.OnPercentChanged; dbManager.RestoreComplete += OnRestoreCompleted; dbManager.RestoreComplete += progressViewModel.OnCompleted; new RadBusyModel().DoWorkAsync(() => { try { dbManager.RestoreDatabase(MyDbUtility.GetConnection(), MyDbUtility.GetDatabaseName(), RestoreFilePath); // these two lines is used to fix the error raised after data restore // error: "Resetting the connection results in a different state than the initial login." MyDbUtility.GetConnection().Close(); MyDbUtility.GetConnection().Open(); } catch (Exception ex) { restoreResult = false; progressViewModel.IsFinished = true; progressViewModel.ValidationResults.Add(new ValidationResult(ex.MostInnerException().Message)); } }); }, closeProgressWindowOnCompleted); return(restoreResult); }