public void OverwriteDBThread(string dynamicsScript, string nonMBScript, string mbScript, string dbPath) { try { _form1.DisableDBControls(false); string bakDescription = tbDBBackupDescription.Text; List <string> runningSQLServer = SQLManagement.GetRunningSQLServers(); if (runningSQLServer.Count > 1) { MessageBox.Show("There are multiple sql servers running. Please stop any sql servers not being used. Environment Manager will target the remaining running sql server."); return; } if (runningSQLServer.Count == 0) { MessageBox.Show("There are no sql servers running. Please start a sql server and try again."); return; } foreach (string server in runningSQLServer) { try { //Directory.Delete(dbPath, true); File.Delete(dbPath + ".zip"); } catch (Exception e1) { MessageBox.Show("Failed deleting the selected db backup " + dbPath + "\n\n" + e1); return; } try { Directory.CreateDirectory(dbPath); } catch (Exception e2) { MessageBox.Show("Failed creating the following directory " + dbPath + "\n\n" + e2); return; } SqlConnection sqlCon = new SqlConnection(@"Data Source=" + Environment.MachineName + "\\" + server + @";Initial Catalog=MASTER;User ID=sa;Password=sa;"); SqlDataAdapter restoreDynScript = new SqlDataAdapter(dynamicsScript, sqlCon); DataTable restoreDynTable = new DataTable(); restoreDynScript.Fill(restoreDynTable); SqlDataAdapter restoreNonMBScript = new SqlDataAdapter(nonMBScript, sqlCon); DataTable restoreNonMBTable = new DataTable(); restoreNonMBScript.Fill(restoreNonMBTable); SqlDataAdapter restoreMBScript = new SqlDataAdapter(mbScript, sqlCon); DataTable restoreMBTable = new DataTable(); restoreMBScript.Fill(restoreMBTable); } using (StreamWriter sw = File.AppendText(dbPath + @"\Description.txt")) { sw.WriteLine("==============================================================================="); sw.WriteLine("BACKUP - " + dbToCreate); sw.WriteLine(DateTime.Now); sw.WriteLine(bakDescription); } // WRITE DATABASE ACTIVITY TO DatabaseActivityLog TABLE DatabaseActivityLogModel databaseActivity = new DatabaseActivityLogModel(Convert.ToString(DateTime.Now), "OVERWRITE", dbToCreate); SqliteDataAccess.WriteDatabaseActivity(databaseActivity); ZipFile.CreateFromDirectory(dbPath, dbPath + ".zip"); string message = "Backup \"" + dbToCreate + "\" was overwritten successfully."; string caption = "COMPLETE"; MessageBoxButtons button = MessageBoxButtons.OK; MessageBoxIcon icon = MessageBoxIcon.Exclamation; DialogResult Result; Result = MessageBox.Show(message, caption, button, icon); _form1.DisableDBControls(true); this.Close(); return; } catch (Exception x1) { MessageBox.Show("There was an exception preforming the backup SQL \n\n" + x1); return; } }
public void NewBackupThread(string dynamicsScript, string nonMBScript, string mbScript, string dbPath, string bakName, string bakDescription) { _form1.DisableDBControls(false); List <string> runningSQLServer = SQLManagement.GetRunningSQLServers(); if (runningSQLServer.Count > 1) { MessageBox.Show("There are multiple sql servers running. Please stop any sql servers not being used. Environment Manager will target the remaining running sql server."); return; } if (runningSQLServer.Count == 0) { MessageBox.Show("There are no sql servers running. Please start a sql server and try again."); return; } foreach (string server in runningSQLServer) { try { Directory.Delete(dbPath, true); } catch (Exception e1) { MessageBox.Show("Failed deleting the selected db backup " + dbPath + "\n\n" + e1); return; } try { Directory.CreateDirectory(dbPath); } catch (Exception e2) { MessageBox.Show("Failed creating the following directory " + dbPath + "\n\n" + e2); return; } SqlConnection sqlCon = new SqlConnection(@"Data Source=" + Environment.MachineName + "\\" + server + @";Initial Catalog=MASTER;User ID=sa;Password=sa;"); SqlDataAdapter restoreDynScript = new SqlDataAdapter(dynamicsScript, sqlCon); DataTable restoreDynTable = new DataTable(); restoreDynScript.Fill(restoreDynTable); SqlDataAdapter restoreNonMBScript = new SqlDataAdapter(nonMBScript, sqlCon); DataTable restoreNonMBTable = new DataTable(); restoreNonMBScript.Fill(restoreNonMBTable); SqlDataAdapter restoreMBScript = new SqlDataAdapter(mbScript, sqlCon); DataTable restoreMBTable = new DataTable(); restoreMBScript.Fill(restoreMBTable); } //catch (SqlException e) //{ // Directory.Delete(dbPath, true); // stopProcess = true; // string errorMessage = "There was an error creating a new Database Backup. \n\nWould you like to view the exception?"; // string errorCaption = "ERROR"; // MessageBoxButtons errorButton = MessageBoxButtons.YesNo; // MessageBoxIcon errorIcon = MessageBoxIcon.Error; // DialogResult errorResult; // errorResult = MessageBox.Show(errorMessage, errorCaption, errorButton, errorIcon); // this.Close(); // if (errorResult == DialogResult.Yes) // { // _form1.DisableDBControls(true); // MessageBox.Show(Convert.ToString(e)); // } // if (errorResult == DialogResult.No) // { // _form1.DisableDBControls(true); // } // return; //} using (StreamWriter sw = File.AppendText(dbPath + @"\Description.txt")) { sw.WriteLine("==============================================================================="); sw.WriteLine("BACKUP - " + bakName); sw.WriteLine(DateTime.Now); sw.WriteLine(bakDescription); } // WRITE DATABASE ACTIVITY TO DatabaseActivityLog TABLE DatabaseActivityLogModel databaseActivity = new DatabaseActivityLogModel(Convert.ToString(DateTime.Now), "CREATED", bakName); SqliteDataAccess.WriteDatabaseActivity(databaseActivity); ZipFile.CreateFromDirectory(dbPath, dbPath + ".zip"); Directory.Delete(dbPath, true); string newMessage = "Backup \"" + bakName + "\" was created successfully."; string newCaption = "COMPLETE"; MessageBoxButtons newButtons = MessageBoxButtons.OK; MessageBoxIcon newIcon = MessageBoxIcon.Exclamation; DialogResult newResult; newResult = MessageBox.Show(newMessage, newCaption, newButtons, newIcon); _form1.DisableDBControls(true); }