Updated the backup and restore functions to be more verbose and stable. Changed the version number slightly for appearance reasons. Fixed a bug on the Main Form that prevented it from refreshing the controls properly after all years have been wiped and after a database restore.

This commit is contained in:
2017-12-10 02:07:52 -06:00
parent 4b96f6c1fa
commit 46bddc7c5f
5 changed files with 204 additions and 31 deletions
+77 -7
View File
@@ -7,7 +7,8 @@ namespace AdvertsingProfitControl
{
public partial class DatabaseRecovery : Form
{
public bool RestoredDatabase;
public bool RestoredDatabase;
private string _informationText = string.Empty;
public DatabaseRecovery()
{
@@ -24,6 +25,11 @@ namespace AdvertsingProfitControl
UpdateBackupList();
}
private void OnBackupLabelClick(object sender, EventArgs e)
{
MessageBox.Show(_informationText, @"Verification Failed Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void UpdateRestoreButtonOnRestoreLocationTextChange(object sender, EventArgs e)
{
restoreBackupButton.Enabled = restoreLocationTextBox.Text.Length != 0;
@@ -65,6 +71,7 @@ namespace AdvertsingProfitControl
{
var backup = new Backup();
var customBackupLocation = string.Empty;
backupResultLabel.Click -= OnBackupLabelClick;
if (!string.IsNullOrWhiteSpace(customBackupLocationTextBox.Text))
{
@@ -87,13 +94,67 @@ namespace AdvertsingProfitControl
if (backup.SingleFileBackup(customBackupLocation))
{
backupResultLabel.Text = @"Successfully created backup(s).";
customBackupLocationTextBox.Text = string.Empty;
//Check the state of the backup.
if (backup.State == 0)
{
backupResultLabel.Text = @"Successfully created backup(s).";
customBackupLocationTextBox.Text = string.Empty;
}
else
{
switch (backup.State)
{
case Backup.PrimaryBackupVerifyFailed:
backupResultLabel.Text = @"Verification failed click to learn more.";
SetVerifcationInformationText("default backup file");
backupResultLabel.Click += OnBackupLabelClick;
break;
case Backup.OptionalBackupVerifyFailed:
backupResultLabel.Text = @"Verification failed click to learn more.";
SetVerifcationInformationText("optional backup file in '" + customBackupLocation + @"'");
backupResultLabel.Click += OnBackupLabelClick;
break;
case Backup.PrimaryBackupVerifyFailed + Backup.OptionalBackupVerifyFailed:
backupResultLabel.Text = @"Verification failed click to learn more.";
SetVerifcationInformationText("default and optional backup (located in " + customBackupLocation + ") files");
backupResultLabel.Click += OnBackupLabelClick;
break;
}
}
}
else
{
MessageBox.Show(backup.ErrorMessage, @"Failed to Create Backup", MessageBoxButtons.OK, MessageBoxIcon.Error);
backupResultLabel.Text = @"Failed to create backup(s).";
switch (backup.State)
{
case Backup.PrimaryBackupFailed:
MessageBox.Show(@"Failed to create the default backup file but if an optional backup location was supplied, that backup was created successfully.", @"Default Backup Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
backupResultLabel.Text = @"Failed to default backup.";
break;
case Backup.OptionalBackupFailed:
MessageBox.Show(@"Default backup was successfully created, however the backup to be created in '" + customBackupLocation + @"' failed to be created.", @"Optional Backup Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
backupResultLabel.Text = @"Failed to create optional backup.";
break;
case Backup.PrimaryBackupFailed + Backup.OptionalBackupFailed:
MessageBox.Show(@"Both the default and the optional backup (in the folder '" + customBackupLocation + @"') failed to be created entirely.", @"Backups Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
backupResultLabel.Text = @"Failed to create backups.";
break;
case Backup.PrimaryBackupFailed + Backup.OptionalBackupVerifyFailed:
MessageBox.Show(@"The default backup file failed to be created and the optional backup (in the folder '" + customBackupLocation + @"') failed verification by the SQL server, click the text to the left of the 'Create Backup' button to learn more.", @"Backup And Verification Errors Detected", MessageBoxButtons.OK, MessageBoxIcon.Error);
backupResultLabel.Text = @"Verification failed click to learn more.";
SetVerifcationInformationText("optional backup (located in " + customBackupLocation + ") file");
backupResultLabel.Click += OnBackupLabelClick;
break;
case Backup.PrimaryBackupVerifyFailed + Backup.OptionalBackupFailed:
MessageBox.Show(@"The optional backup file (in the folder '" + customBackupLocation + @"') failed to be created. The default backup file failed verification by the SQL server, click the text to the left of the 'Create Backup' button to learn more.", @"Backup And Verification Errors Detected", MessageBoxButtons.OK, MessageBoxIcon.Error);
backupResultLabel.Text = @"Verification failed click to learn more.";
SetVerifcationInformationText("default backup file");
backupResultLabel.Click += OnBackupLabelClick;
break;
default:
MessageBox.Show(backup.ErrorMessage, @"Unknown Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
break;
}
}
UpdateBackupList();
@@ -135,7 +196,7 @@ namespace AdvertsingProfitControl
return;
}
if (restore.SingleFileRestore(restoreLocationTextBox.Text))
if (restore.SingleFileRestore(restoreLocationTextBox.Text, Properties.Settings.Default.DefaultServer))
{
restoreResultLabel.Text = @"Successfully restored the database.";
restoreLocationTextBox.Text = string.Empty;
@@ -144,7 +205,7 @@ namespace AdvertsingProfitControl
else
{
MessageBox.Show(restore.ErrorMessage, @"Failed to Restore Backup", MessageBoxButtons.OK, MessageBoxIcon.Error);
restoreInfoLabel.Text = @"Failed to restore the database.";
restoreResultLabel.Text = @"Failed to restore the database.";
}
}
@@ -172,5 +233,14 @@ namespace AdvertsingProfitControl
UpdateBackupList();
}
private void SetVerifcationInformationText(string backupType)
{
_informationText = @"The recently created " + backupType + " failed verification checks by the SQL Server."
+ Environment.NewLine +
@"This is shown by the '(Verification Failed)' text in the 'Current Backups' column. This means that the backup data could be damaged." +
Environment.NewLine +
@"While it is NOT recommended that you use this backup data to restore the database you may do so but it could result in data loss.";
}
}
}