/// <summary> /// 备份数据库 /// </summary> /// <param name="path">备份文件地址如D://abc.sql</param> /// <returns></returns> public static bool BackupDb(object path) { bool isSuccess = false; Stream someStream = new MemoryStream(); try { MySqlConnection myconn = new MySqlConnection(AppSettings.GetEntityValue("Bets:UrlAddress")); if (myconn.State == ConnectionState.Closed) { myconn.Open(); } try { using (myconn) { var command = myconn.CreateCommand(); var backupProvider = new SqlBackup(command); backupProvider.BackupDb(someStream); isSuccess = true; } } catch (Exception ex) { LogHelper.Error($"BackupDB_备份数据库异常。{ex.Message}", "SQLServerIMPL"); } finally { if (myconn.State == ConnectionState.Open) { myconn.Close(); myconn.Dispose(); } } } catch (Exception ex) { LogHelper.Error($"BackupDB_备份数据库异常。{ex.Message}", "SQLServerIMPL"); } return(isSuccess); }
/// <summary> /// 还原数据库 /// </summary> /// <param name="path">指定还原文件***.sql的绝对路径</param> /// <param name="dbName">还原到指定数据库</param> /// <returns></returns> public static bool RestoreDb(string path, string dbName) { bool isSuccess = false; Stream someStream = new MemoryStream(); try { MySqlConnection myconn = new MySqlConnection(AppSettings.GetEntityValue("Bets:UrlAddress")); if (myconn.State == ConnectionState.Closed) { myconn.Open(); } try { using (myconn) { var command = myconn.CreateCommand(); // To Create a SQLBackup instance, you need an instance of IDBCommand var backupProvider = new SqlBackup(command); backupProvider.RestoreDb(someStream); isSuccess = true; } } catch (Exception ex) { LogHelper.Error($"BackupDB_还原数据库异常。{ex.Message}", "SQLServerIMPL"); } finally { if (myconn.State == ConnectionState.Open) { myconn.Close(); myconn.Dispose(); } } } catch (Exception ex) { LogHelper.Error($"BackupDB_还原数据库异常。{ex.Message}", "SQLServerIMPL"); } return(isSuccess); }
public void BtnBackup_Click(object sender, RoutedEventArgs e) { try { Random random = new Random(); string currentBackupFileName = DateTime.Now.Day + "-" + DateTime.Now.Month + "-" + DateTime.Now.Year + "-" + FileNames.FILENAME_BACKUP + "-" + random.Next(1000, 9999) + ".bak"; string path = FileNames.BACKUP_FILE_PATH + @"\" + currentBackupFileName; SqlBackup.Backup(path); Backup backup = new Backup() { filename = currentBackupFileName, filepath = path, user_id = User.id }; int id = DB.AddBackup(backup); if (id > 0) { backupList.Items.Add(currentBackupFileName + " / " + id.ToString()); result.Background = Brushes.Green; result.Content = Lang.BackupSuccessfully; FillListBox(); } else { result.Background = Brushes.Red; result.Content = Lang.BackupFailed; } } catch (Exception ex) { DB.AddLog(new Log() { error_page = "backup_buttonbackup", error_text = ex.Message, log_date = DateTime.Now, log_user = User.id }); } }
private void BtnRestore_Click(object sender, RoutedEventArgs e) { try { if (backupList.SelectedIndex == -1) { result.Background = Brushes.Red; result.Content = Lang.BackupSelectListBox; } else { string filename = backupList.SelectedItem.ToString(); string[] arr = filename.Split('/'); int id = int.Parse(arr[1]); Backup selectedBackup = DB.GetBackup(User, id); result.Background = Brushes.Green; result.Content = Lang.BackupRestoreSuccessfully; Backups = DB.GetBackups(User); SqlBackup.Restore(selectedBackup.filepath); // restore current selected backup DB.AddBackupRange(Backups); // the backup table must not be deleted mainWindow.Menu_userLogout_Click(sender, e); } } catch (Exception ex) { DB.AddLog(new Log() { error_page = "backup_buttonbackup", error_text = ex.Message, log_date = DateTime.Now, log_user = User.id }); } }