private void btnClearCache_Click(object sender, EventArgs e) { if (!btnClearCache.Enabled) { return; } Cache.Clear(); MsgBoxEx.Inform("Cached application data has been cleared.", "Clear Cache"); }
private void RunRestoration() { if (!File.Exists(_backupfilename)) { MsgBoxEx.Shout("Could not locate the specified database backup file / restore point.", "Database Restoration"); return; } btnBrowseDrive.Enabled = false; btnBrowseRestorePoint.Enabled = false; btnRestore.Enabled = false; btnCancel.BringToFront(); InitializeEventGrid(); chkCreateRestorePoint.Enabled = false; pctLoad.Show(); pctLoad.BringToFront(); _isrunning = true; _cancelled = false; AddEvent(BackupEvent.Information, "Starting database restoration routines..."); RestorePointInfo _restorepoint = null; if (chkCreateRestorePoint.Checked) { _restorepoint = MakeRestorePoint(); if (_restorepoint == null) { btnBrowseDrive.Enabled = true; btnBrowseRestorePoint.Enabled = true; btnRestore.Enabled = true; btnCancel.SendToBack(); chkCreateRestorePoint.Enabled = true; pctLoad.Hide(); _isrunning = false; Materia.RefreshAndManageCurrentProcess(); return; } } string _filename = _backupfilename; int _trycounter = 0; if (Path.GetExtension(_filename).ToLower().Replace(".", "").RLTrim() == "scmsiv") { AddEvent(BackupEvent.Information, "Extracting database backup..."); _filename = ""; string _dbtempdir = Application.StartupPath + "\\dbtemp"; if (!Directory.Exists(_dbtempdir)) { try { Directory.CreateDirectory(_dbtempdir); } catch (Exception ex) { SCMS.LogError(this.GetType().Name, ex); AddEvent(BackupEvent.Error, "Could not create temporary backup file extraction directory."); btnBrowseDrive.Enabled = true; btnBrowseRestorePoint.Enabled = true; btnRestore.Enabled = true; btnCancel.SendToBack(); chkCreateRestorePoint.Enabled = true; pctLoad.Hide(); _isrunning = false; Materia.RefreshAndManageCurrentProcess(); return; } } string _archivedir = _dbtempdir + "\\" + Path.GetFileNameWithoutExtension(_backupfilename); Func <string, string, bool> _extractordelegate = new Func <string, string, bool>(Archiver.Decompress); IAsyncResult _extractorresult = _extractordelegate.BeginInvoke(_backupfilename, _archivedir, null, _extractordelegate); while (!_extractorresult.IsCompleted && !_cancelled) { Thread.Sleep(1); Application.DoEvents(); } if (_cancelled) { AddEvent(BackupEvent.Warning, "Cancelling database restoration..."); _trycounter = 0; while (Directory.Exists(_archivedir) && _trycounter <= 30) { try { Directory.Delete(_archivedir, true); } catch { } _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } btnBrowseDrive.Enabled = true; btnBrowseRestorePoint.Enabled = true; btnRestore.Enabled = true; btnCancel.SendToBack(); chkCreateRestorePoint.Enabled = true; pctLoad.Hide(); _isrunning = false; Materia.RefreshAndManageCurrentProcess(); AddEvent(BackupEvent.Information, "Cancelled database restoration."); _trycounter = 0; while (_trycounter < 10) { Thread.Sleep(100); Application.DoEvents(); _trycounter += 1; } } else { bool _extracted = _extractordelegate.EndInvoke(_extractorresult); if (_extracted) { if (Directory.Exists(_archivedir)) { string[] _files = Directory.GetFiles(_archivedir); foreach (string _file in _files) { if (Path.GetExtension(_file).ToLower().Replace(".", "").RLTrim() == "sql") { _filename = _file; break; } } if (String.IsNullOrEmpty(_filename.RLTrim())) { AddEvent(BackupEvent.Error, "Could not find any supported database backup file."); _trycounter = 0; while (Directory.Exists(_archivedir) && _trycounter <= 30) { try { Directory.Delete(_archivedir, true); } catch { } _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } btnBrowseDrive.Enabled = true; btnBrowseRestorePoint.Enabled = true; btnRestore.Enabled = true; btnCancel.SendToBack(); chkCreateRestorePoint.Enabled = true; pctLoad.Hide(); _isrunning = false; Materia.RefreshAndManageCurrentProcess(); return; } } else { AddEvent(BackupEvent.Error, "Could not extract data from database backup."); _trycounter = 0; while (Directory.Exists(_archivedir) && _trycounter <= 30) { try { Directory.Delete(_archivedir, true); } catch { } _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } btnBrowseDrive.Enabled = true; btnBrowseRestorePoint.Enabled = true; btnRestore.Enabled = true; btnCancel.SendToBack(); chkCreateRestorePoint.Enabled = true; pctLoad.Hide(); _isrunning = false; Materia.RefreshAndManageCurrentProcess(); return; } } else { AddEvent(BackupEvent.Error, "Could not extract data from database backup."); _trycounter = 0; while (Directory.Exists(_archivedir) && _trycounter <= 30) { try { Directory.Delete(_archivedir, true); } catch { } _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } btnBrowseDrive.Enabled = true; btnBrowseRestorePoint.Enabled = true; btnRestore.Enabled = true; btnCancel.SendToBack(); chkCreateRestorePoint.Enabled = true; pctLoad.Hide(); _isrunning = false; Materia.RefreshAndManageCurrentProcess(); return; } } } if (!String.IsNullOrEmpty(_filename.RLTrim())) { FileInfo _backupfile = new FileInfo(_filename); Func <string, FileInfo, MySqlResult> _restoredelegate = new Func <string, FileInfo, MySqlResult>(MySql.Execute); IAsyncResult _restoreresult = _restoredelegate.BeginInvoke(SCMS.ServerConnection.ToString(), _backupfile, null, _restoredelegate); while (!_restoreresult.IsCompleted && !_cancelled) { Thread.Sleep(1); Application.DoEvents(); } if (_cancelled) { AddEvent(BackupEvent.Warning, "Cancelling database restoration..."); if (_restorepoint != null) { if (File.Exists(_restorepoint.Filename)) { AddEvent(BackupEvent.Information, "Performing roll back from recorded restore point."); FileInfo _restorepointfile = new FileInfo(_restorepoint.Filename); _trycounter = 0; while (_trycounter < 30) { _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } Func <string, FileInfo, MySqlResult> _restorepointdelegate = new Func <string, FileInfo, MySqlResult>(MySql.Execute); IAsyncResult _restorepointresult = _restorepointdelegate.BeginInvoke(SCMS.ServerConnection.ToString(), _restorepointfile, null, _restorepointdelegate); _restorepointresult.WaitToFinish(); MySqlResult _rpresult = _restorepointdelegate.EndInvoke(_restorepointresult); if (_rpresult.Succeeded) { AddEvent(BackupEvent.Success, "Roll back from recorded restore point has been completed."); } else { SCMS.LogError(this.GetType().Name, new Exception(_rpresult.Error)); AddEvent(BackupEvent.Error, "Failed to roll back from restore point."); } } } SCMS.CleanUp(); btnBrowseDrive.Enabled = true; btnBrowseRestorePoint.Enabled = true; btnRestore.Enabled = true; btnCancel.SendToBack(); chkCreateRestorePoint.Enabled = true; pctLoad.Hide(); _isrunning = false; Materia.RefreshAndManageCurrentProcess(); AddEvent(BackupEvent.Information, "Cancelled database restoration."); _trycounter = 0; while (_trycounter < 30) { Thread.Sleep(100); Application.DoEvents(); _trycounter += 1; } } else { MySqlResult _execresult = _restoredelegate.EndInvoke(_restoreresult); if (_execresult.Succeeded) { AddEvent(BackupEvent.Information, "Finalizing database restoration..."); IAsyncResult _logresult = SCMS.CurrentSystemUser.LogActionAsync(UserAction.RestoreDatabase, "Restored database from file : " + lblPath.Text.ToSqlValidString().Replace("\\\\", "\\") + "."); IAsyncResult _queexresult = Que.BeginExecution(SCMS.ServerConnection.ToString(), "UPDATE `settings` SET `LastRestored` = NOW();"); while (!_logresult.IsCompleted && !_queexresult.IsCompleted) { Thread.Sleep(1); Application.DoEvents(); } QueResult _queresult = Que.EndExecution(_queexresult); _queresult.Dispose(QueResultDisposition.WithAssociatedQue); SCMS.CleanUp(); _isrunning = false; AddEvent(BackupEvent.Success, "Database backup restoration has been completed."); _trycounter = 0; while (_trycounter <= 10) { Thread.Sleep(100); Application.DoEvents(); _trycounter += 1; } MsgBoxEx.Inform("Application will restart for the restored values to fully take effect.", "Database Backup Restoration"); DialogResult = System.Windows.Forms.DialogResult.OK; Close(); } else { SCMS.LogError(this.GetType().Name, new Exception(_execresult.Error)); AddEvent(BackupEvent.Error, "Failed to complete database restoration from the specified database backup / restore point."); if (_restorepoint != null) { if (File.Exists(_restorepoint.Filename)) { AddEvent(BackupEvent.Information, "Performing roll back from recorded restore point."); FileInfo _restorepointfile = new FileInfo(_restorepoint.Filename); _trycounter = 0; while (_trycounter < 15) { _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } Func <string, FileInfo, MySqlResult> _restorepointdelegate = new Func <string, FileInfo, MySqlResult>(MySql.Execute); IAsyncResult _restorepointresult = _restorepointdelegate.BeginInvoke(SCMS.ServerConnection.ToString(), _restorepointfile, null, _restorepointdelegate); _restorepointresult.WaitToFinish(); MySqlResult _rpresult = _restorepointdelegate.EndInvoke(_restorepointresult); if (_rpresult.Succeeded) { AddEvent(BackupEvent.Success, "Roll back from recorded restore point has been completed."); } else { SCMS.LogError(this.GetType().Name, new Exception(_rpresult.Error)); AddEvent(BackupEvent.Error, "Failed to roll back from restore point."); } } } SCMS.CleanUp(); btnBrowseDrive.Enabled = true; btnBrowseRestorePoint.Enabled = true; btnRestore.Enabled = true; btnCancel.SendToBack(); chkCreateRestorePoint.Enabled = true; pctLoad.Hide(); _isrunning = false; Materia.RefreshAndManageCurrentProcess(); } } } }
private void btnTestConnection_Click(object sender, EventArgs e) { if (!btnTestConnection.Enabled) { return; } Validator _validator = SCMS.Validators[this]; if (!Materia.Valid(_validator, txtUserId, !String.IsNullOrEmpty(txtUserId.Text.RLTrim()), "Please specify database server's log on account id.")) { return; } if (!Materia.Valid(_validator, txtPassword1, !String.IsNullOrEmpty(txtPassword1.Text.RLTrim()), "Please specify database server's log on account password.")) { return; } if (!Materia.Valid(_validator, txtPassword2, string.Compare(txtPassword1.Text, txtPassword2.Text) == 0 && txtPassword1.Text == txtPassword2.Text, "Passwords doesn't match each other.")) { return; } btnTestConnection.Enabled = false; btnOk.Enabled = false; txtUserId.ReadOnly = true; txtPassword1.ReadOnly = true; txtPassword2.ReadOnly = true; string _connectionstring = "SERVER=" + txtServer.Text + ";DATABASE=" + txtDatabase.Text + ";UID=" + txtUserId.Text + ";PWD=" + txtPassword1.Text + ";"; IDbConnection _connection = Development.Materia.Database.Database.CreateConnection(_connectionstring); Func <IDbConnection, bool> _delegate = new Func <IDbConnection, bool>(Materia.CanConnect); IAsyncResult _result = _delegate.BeginInvoke(_connection, null, _delegate); while (!_result.IsCompleted && !_cancelled) { Thread.Sleep(1); Application.DoEvents(); } Materia.RefreshAndManageCurrentProcess(); if (_cancelled) { if (!_result.IsCompleted) { try { _result = null; } catch { } finally { Materia.RefreshAndManageCurrentProcess(); } } } else { bool _connected = _delegate.EndInvoke(_result); if (_connected) { MsgBoxEx.Inform("Connection has been successfully established.", "Test Database Connection"); } else { MsgBoxEx.Alert("Failed to establish a connection using the specified configurations.", "Connection Failed"); } } if (_connection != null) { if (_connection.State == ConnectionState.Open) { try { _connection.Close(); } catch { } } try { _connection.Dispose(); } catch { } finally { _connection = null; Materia.RefreshAndManageCurrentProcess(); } } btnTestConnection.Enabled = true; btnOk.Enabled = true; txtUserId.ReadOnly = false; txtPassword1.ReadOnly = false; txtPassword2.ReadOnly = false; }