/// <summary> /// Method to restore the database. /// It opens a open file dialog to choose from where to load the backup. /// We use the ADO connection to backup and restore the database. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ButtonRestoreDatabase_Click(object sender, EventArgs e) { // opening the file dialog OpenFileDialog openFileDialog = new OpenFileDialog { // we start up in the debug directory, go two levels up to get to the main project area // note need to use Path.GetFullPath() as InitialDirectory does not like relative directories InitialDirectory = Path.GetFullPath(Application.StartupPath + "\\..\\.."), Filter = "XML files (*.xml)|*.xml", Title = "Restore the backup" }; //Passing "this" to show dialog, so the main form can be //focused after the dialog is hidden if (openFileDialog.ShowDialog(this) == DialogResult.OK && openFileDialog.FileName != "") { CleanDatabaseAndEntities(); BusinessMarketplaceDataAccessLayer businessMarketplaceDataAccessLayer = new BusinessMarketplaceDataAccessLayer(); string connectionString = businessMarketplaceDataAccessLayer.GetConnectionString("BusinessMarketplaceSqlProvider"); businessMarketplaceDataAccessLayer.OpenConnection(connectionString); DataSet businessMarketplaceDataSet = businessMarketplaceDataAccessLayer.InitDataSet(); var result = false; var message = "There was an error loading the backup."; try { result = businessMarketplaceDataAccessLayer.RestoreDataSetFromBackup(businessMarketplaceDataSet, openFileDialog.FileName); } catch (Exception exception) { Debug.Print(exception.StackTrace); } businessMarketplaceDataAccessLayer.CloseConnection(); LoadEntities(); //For Business correct load in business tab SetBusinessComboBox(); if (result) { message = "Backup successfully loaded."; } MessageBox.Show(message); } }
/// <summary> /// Method to backup the database. /// We use the ADO connection to backup and restore the database. /// It opens a save file dialog to choose where to save the backup, /// it also suggest to use a timestamp with the database name to save the backup. /// The timestamp uses dashes for the time, since using colons in the name of a file /// is forbidden. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ButtonBackupDatabase_Click(object sender, EventArgs e) { // opening the save file dialog SaveFileDialog saveFileDialog = new SaveFileDialog { // we start up in the debug directory, go two levels up to get to the main project area // note need to use Path.GetFullPath() as InitialDirectory does not like relative directories InitialDirectory = Path.GetFullPath(Application.StartupPath + "\\..\\.."), Filter = "XML files (*.xml)|*.xml", Title = "Save the backup", FileName = "BusinessMarketplaceBackup " + DateTime.Now.ToString("yyyy-MM-dd HH-mm") }; //Passing "this" to show dialog, so the main form can be //focused after the dialog is hidden if (saveFileDialog.ShowDialog(this) == DialogResult.OK && saveFileDialog.FileName != "") { BusinessMarketplaceDataAccessLayer businessMarketplaceDataAccessLayer = new BusinessMarketplaceDataAccessLayer(); string connectionString = businessMarketplaceDataAccessLayer.GetConnectionString("BusinessMarketplaceSqlProvider"); businessMarketplaceDataAccessLayer.OpenConnection(connectionString); DataSet businessMarketplaceDataSet = businessMarketplaceDataAccessLayer.InitDataSet(); var result = false; var message = "There was an error saving the backup."; try { result = businessMarketplaceDataAccessLayer.BackupDataSetToXML(businessMarketplaceDataSet, saveFileDialog.FileName); } catch (Exception exception) { Debug.Print(exception.StackTrace); } businessMarketplaceDataAccessLayer.CloseConnection(); if (result) { message = "Backup successfully saved."; } MessageBox.Show(message); } }