//private void GetDatabases() //{ // //Retrieve the available Databases. // List<string> objDBNames = DBSettings.GetDatabases(cmbServers.SelectedItem.ToString(), txtUser.Text, txtPassword.Text); // if (objDBNames != null) // { // for (int i = 0; i < objDBNames.Count; i++) // { // cmbDatabases.Items.Add(objDBNames[i]); // } // } // if (this.cmbDatabases.Items.Count > 0) // this.cmbDatabases.SelectedIndex = 0; // else // this.cmbDatabases.Text = "NorthWind"; //} private void btnRestore_Click(object sender, EventArgs e) { try { bool bResult = false; btnRestore.Enabled = false; string strDataBase = string.Empty; Dictionary <string, string> objServer = new Dictionary <string, string>(); objServer.Add("SERVER", cmbServers.SelectedItem.ToString()); objServer.Add("UID", txtUser.Text); objServer.Add("PASSWORD", txtPassword.Text); strDataBase = txtDataBases.Text; objServer.Add("DATABASE", strDataBase); if (ConfigManager.Read("Startuppath") != null) { txtDBFile.Text = ConfigManager.Read("Startuppath"); } objServer.Add("LOCATION", txtDBFile.Text); LogManager.WriteLog("LOCATION:" + txtDBFile.Text.ToString(), LogManager.enumLogLevel.Debug); bool bDBExists = false; string sSQLServerDetails = GetConnectionString(); bDBExists = DBSettings.CheckDBExists(sSQLServerDetails, strDataBase, 60); if (bDBExists == true) { MessageBox.Show(strDataBase + " has not been restored. The DB already exists!", "Restore DB", MessageBoxButtons.OK, MessageBoxIcon.Information); LogManager.WriteLog(strDataBase + " has not been restored. The DB already exists!", LogManager.enumLogLevel.Info); } else { MessageBox.Show("The Restore Process Running for " + strDataBase + "." + " Wait for the success dialog before doing anything else!", "Restore DB", MessageBoxButtons.OK, MessageBoxIcon.Information); bResult = DBSettings.RestoreDB(strType, objServer); if (bResult) { //MessageBox.Show("The database " + strDataBase + " has been restored.", "Restore DB", MessageBoxButtons.OK, MessageBoxIcon.Information); LogManager.WriteLog("The database " + strDataBase + " has been restored.", LogManager.enumLogLevel.Info); } else { MessageBox.Show("The Restore Process failed for " + strDataBase + ".", "Restore DB", MessageBoxButtons.OK, MessageBoxIcon.Error); LogManager.WriteLog("The Restore Process failed for " + strDataBase + ".", LogManager.enumLogLevel.Info); } } } catch (Exception ex) { ExceptionManager.Publish(ex); LogManager.WriteLog("btnRestore_Click:" + ex.Message + ex.Source.ToString(), LogManager.enumLogLevel.Error); } finally { btnRestore.Enabled = true; } }
private void btnRestore_Click(object sender, RoutedEventArgs e) { string blankDBFile = string.Empty; string dbScriptsDefaultPath = string.Empty; string backupFileName = string.Empty; string sSQLServerDetails = string.Empty; bool bDBExists = false; try { Cursor = System.Windows.Input.Cursors.Wait; sSQLServerDetails = GetConnectionString(); bDBExists = DBSettings.CheckDBExists(sSQLServerDetails, txtDataBases.Text, 60); if (bDBExists == true) { MessageBox.ShowText(string.Format("{0} {1}", txtDataBases.Text, FindResource("MessageID50")), BMC_Icon.Information); LogManager.WriteLog(string.Format("{0} {1}", txtDataBases.Text, FindResource("MessageID50")), LogManager.enumLogLevel.Info); } else { dbScriptsDefaultPath = ConfigurationManager.AppSettings.Get("DBScriptsDefaultPath"); if (this.strType.ToUpper() == "EXCHANGE") { blankDBFile = ConfigurationManager.AppSettings.Get("ExchangeBlankDB"); } if (this.strType.ToUpper() == "TICKETING") { blankDBFile = ConfigurationManager.AppSettings.Get("TicketingBlankDB"); } if (this.strType.ToUpper() == "EXTSYSMSG") { blankDBFile = ConfigurationManager.AppSettings.Get("EXTSYSMSGBlankDB"); } backupFileName = string.Format("{0}\\{1}", dbScriptsDefaultPath, blankDBFile); // Create a new connection to the Server ServerConnection serverConnection = new ServerConnection(cmbServers.SelectedItem.ToString()); // Log in using SQL authentication instead of Windows authentication serverConnection.LoginSecure = false; // Give the login username serverConnection.Login = txtUsername.Text; // Give the login password serverConnection.Password = txtPassword.Password; // Create a new SQL Server object using the connection we created Server server = new Server(serverConnection); // Create a new database restore operation Restore rstDatabase = new Restore(); // Set the restore type to a database restore rstDatabase.Action = RestoreActionType.Database; // Set the database that we want to perform the restore on rstDatabase.Database = txtDataBases.Text; // Set the backup device from which we want to restore the db BackupDeviceItem bkpDevice = new BackupDeviceItem(backupFileName, DeviceType.File); // Add the backup device to the restore type rstDatabase.Devices.Add(bkpDevice); // Optional. ReplaceDatabase property ensures that any existing copy of the database is overwritten. rstDatabase.ReplaceDatabase = true; // Perform the restore rstDatabase.SqlRestore(server); LogManager.WriteLog(string.Format("{0} - {1}", "Database Restore Complete. Running Updgrade Scripts for database", txtDataBases.Text), LogManager.enumLogLevel.Info); if (this.strType.ToUpper() == "EXCHANGE" | this.strType.ToUpper() == "TICKETING") { if (Convert.ToBoolean(ConfigurationManager.AppSettings.Get("AutoRunUpgradeScriptAfterDBRestore"))) { try { RunDatabaseUpgradeScripts(); } catch (Exception ex) { ExceptionManager.Publish(ex); if (this.strType.ToUpper() == "EXCHANGE") { MessageBox.ShowBox("MessageID106", BMC_Icon.Error); } if (this.strType.ToUpper() == "TICKETING") { MessageBox.ShowBox("MessageID107", BMC_Icon.Error); } } } LogManager.WriteLog(string.Format("{0} - {1}", "Updgrade Scripts run successfully for database", txtDataBases.Text), LogManager.enumLogLevel.Info); } MessageBox.ShowText(string.Format("{0} {1} {2}.", FindResource("MessageID53"), txtDataBases.Text, FindResource("MessageID54")), BMC_Icon.Information); } } catch (Exception ex) { MessageBox.ShowBox("MessageID74", BMC_Icon.Error); ExceptionManager.Publish(ex); } finally { btnRestore.IsEnabled = true; Cursor = System.Windows.Input.Cursors.Arrow; } }