/// <summary> /// Connects to the current database. /// </summary> /// <param name="server">The database server.</param> /// <param name="userState">The user state.</param> protected void DatabaseConnect(DbServer server, object userState = null) { // Call the connecting method. this.OnConnectStarted(server); // Show a connecting message. this.ShowMessage(Resources.Connect_48, "Database", "Connecting to the database server \'{0}\'...".FormatWith(server.Name)); try { // Connect asynchronously to the database server. server.Open(this.DatabaseConnected, userState); } catch (Exception exception) { // If an exception occurs, hide the connecting message. this.HideMessage(); // Call the on connect failed method. this.OnConnectFailed(server); // Display an error message box to the user. MessageBox.Show( this, "Connecting to the database server \'{0}\' failed. {1}".FormatWith(server.Name, exception.Message), "Connecting to Database Failed", MessageBoxButtons.OK, MessageBoxIcon.Error ); } }
/// <summary> /// Returns the server name to display in the tree view. /// </summary> /// <param name="server">The server.</param> /// <returns>The server name</returns> private string GetServerTreeName(DbServer server) { return server.Name + (this.crawler.Database.Sql.IsPrimary(server) ? " (primary)" : string.Empty); }
// Protected methods. /// <summary> /// A method called when connecting to the database server succeeded. /// </summary> /// <param name="server">The database server.</param> protected override void OnConnectSucceeded(DbServer server) { // Enable the refresh database button. this.buttonDatabaseRefresh.Enabled = true; }
/// <summary> /// A method called when connecting to the database server failed. /// </summary> /// <param name="server">The database server.</param> protected override void OnConnectFailed(DbServer server) { // Disable the refresh database button. this.buttonDatabaseRefresh.Enabled = false; }
/// <summary> /// Creates a new event arguments instance. /// </summary> /// <param name="oldPrimary">The old primary database server.</param> /// <param name="newPrimary">The new primary database server.</param> public DbPrimaryServerChangedEventArgs(DbServer oldPrimary, DbServer newPrimary) { this.OldPrimary = oldPrimary; this.NewPrimary = newPrimary; }
/// <summary> /// Creates a new event arguments instance. /// </summary> /// <param name="server">The database server.</param> public DbServerEventArgs(DbServer server) { this.Server = server; }
/// <summary> /// A method called when disconnecting from the database has failed. /// </summary> /// <param name="server">The database server.</param> protected virtual void OnDisconnectFailed(DbServer server) { }
// Public methods. /// <summary> /// Call this method to update the state of the database server. /// </summary> /// <param name="server">The database server.</param> public void StateChanged(DbServer server) { // If the server is different from the current one, do nothing. if (server != this.server) return; // Else, update the state. this.pictureBox.Image = ControlServerProperties.images[(int)server.State]; }
/// <summary> /// A method called when connecting to the database has succeeded. /// </summary> /// <param name="server">The database server.</param> protected override void OnConnectSucceeded(DbServer server) { // Enable the buttons. this.buttonRefresh.Enabled = true; this.buttonClose.Enabled = true; // Raise the database operation finished event. if (this.DatabaseOperationFinished != null) this.DatabaseOperationFinished(this, EventArgs.Empty); }
// Protected methods. /// <summary> /// A method called when started connecting to the database server. /// </summary> /// <param name="server">The database server.</param> protected override void OnConnectStarted(DbServer server) { // Disable the buttons. this.buttonRefresh.Enabled = false; this.buttonClose.Enabled = false; // Raise the database operation started event. if (this.DatabaseOperationStarted != null) this.DatabaseOperationStarted(this, EventArgs.Empty); }
/// <summary> /// Changes the password of the specified database server. /// </summary> /// <param name="server">The database server.</param> protected void DatabaseChangePassword(DbServer server) { // Change the password for the selected server. this.formChangePassword.ShowDialog(this, server.Password, server); }
/// <summary> /// A method called when disconnecting from the database has succeeded. /// </summary> /// <param name="server">The database server.</param> protected virtual void OnDisconnectSucceeded(DbServer server) { }
/// <summary> /// A method called when started disconnecting from the database server. /// </summary> /// <param name="server">The database server.</param> protected virtual void OnDisconnectStarted(DbServer server) { }
/// <summary> /// Creates a new event arguments instance. /// </summary> /// <param name="oldState">The old state.</param> /// <param name="newState">The new state.</param> public DbServerStateEventArgs(DbServer server, DbServer.ServerState oldState, DbServer.ServerState newState) : base(server) { this.OldState = oldState; this.NewState = newState; }
// Protected methods. /// <summary> /// A method called when started connecting to the database server. /// </summary> /// <param name="server">The database server.</param> protected override void OnConnectStarted(DbServer server) { // Disable the control. this.tabControl.Enabled = false; // Raise the database operation started event. if (this.DatabaseOperationStarted != null) this.DatabaseOperationStarted(this, EventArgs.Empty); }
/// <summary> /// Executes a database query. /// </summary> /// <param name="server">The database server.</param> /// <param name="query">The database query.</param> protected override void DatabaseQuery(DbServer server, DbQuery query) { // If the server is not an SQL server, do nothing. if (!(server is DbServerSql)) return; // If the query is not an SQL query, do nothing. if (!(query is DbQuerySql)) return; // Get the SQL server. DbServerSql serverSql = server as DbServerSql; // Get the SQL query. DbQuerySql querySql = query as DbQuerySql; // If the database server is not connected. if (serverSql.State != DbServerSql.ServerState.Connected) { // Connect to the database and pass the query as the user state. this.DatabaseConnect(serverSql, querySql); // Return. return; } // Else, create a database command that selects all items for the specified table. // Show a connecting message. this.ShowMessage(Resources.DatabaseBusy_48, "Database", querySql.MessageStart); // Create a new database command. DbCommand command = serverSql.CreateCommand(querySql); // Call the query start method. this.OnQueryStarted(serverSql, querySql, command); try { // Execute the command asynchronously. command.ExecuteReader((DbAsyncResult commandResult, DbReader reader) => { try { // If the result has an exception, throw the exception. if (commandResult.Exception != null) throw commandResult.Exception; // Read the data asynchronously for the specified query. reader.Read(querySql, null, (DbAsyncResult readerResult, DbData result) => { try { // Throw a reader exception, if any. if (readerResult.Exception != null) { reader.Close(); throw readerResult.Exception; } // Get the number of records read. int recordsAffected = reader.RecordsAffected; // Close the reader. reader.Close(); // Dispose and reset the command. command.Dispose(); // Show a success message. this.ShowMessage(Resources.DatabaseSuccess_48, "Database", querySql.MessageFinishSuccess, false); // Wait. Thread.Sleep(ApplicationConfig.MessageCloseDelay); // Hide the message. this.HideMessage(); // Call the completion method, depending on the type of data. if (querySql.Table != null) this.DatabaseQuerySuccess(serverSql, querySql, result as DbDataObject, recordsAffected); else this.DatabaseQuerySuccess(serverSql, querySql, result as DbDataRaw, recordsAffected); } catch (Exception exception) { // Dispose the command. command.Dispose(); // Show an error message. this.ShowMessage(Resources.DatabaseError_48, "Database", querySql.MessageFinishFail, false); // Log the event. serverSql.LogEvent( LogEventLevel.Important, LogEventType.Error, "Executing query on the database server \'{0}\' failed. {1}", new object[] { serverSql.Name, exception.Message }, exception); // Wait. Thread.Sleep(ApplicationConfig.MessageCloseDelay); // Hide the message. this.HideMessage(); // Call the completion method. this.DatabaseQueryFail(serverSql, querySql, exception); } }, null); } catch (DbException exception) { // Dispose the command. command.Dispose(); // Show an error message. this.ShowMessage(Resources.DatabaseError_48, "Database", "{0} {1}".FormatWith(querySql.MessageFinishFail, exception.InnerException.Message), false); // Log the event. serverSql.LogEvent( LogEventLevel.Important, LogEventType.Error, "Executing query on the database server \'{0}\' failed. {1}", new object[] { serverSql.Name, exception.Message }, exception); // Wait. Thread.Sleep(ApplicationConfig.MessageCloseDelay); // Hide the message. this.HideMessage(); // Call the completion method. this.DatabaseQueryFail(serverSql, querySql, exception); } catch (Exception exception) { // Dispose the command. command.Dispose(); // Show an error message. this.ShowMessage(Resources.DatabaseError_48, "Database", querySql.MessageFinishFail, false); // Log the event. serverSql.LogEvent( LogEventLevel.Important, LogEventType.Error, "Executing query on the database server \'{0}\' failed. {1}", new object[] { serverSql.Name, exception.Message }, exception); // Wait. Thread.Sleep(ApplicationConfig.MessageCloseDelay); // Hide the message. this.HideMessage(); // Call the completion method. this.DatabaseQueryFail(serverSql, querySql, exception); } }); } catch (Exception exception) { // Dispose the command. command.Dispose(); // Show an error message. this.ShowMessage(Resources.DatabaseError_48, "Database", querySql.MessageFinishFail, false); // Log the event. serverSql.LogEvent( LogEventLevel.Important, LogEventType.Error, "Executing query on the database server \'{0}\' failed. {1}", new object[] { serverSql.Name, exception.Message }, exception); // Wait. Thread.Sleep(ApplicationConfig.MessageCloseDelay); // Hide the message. this.HideMessage(); // Call the completion method. this.DatabaseQueryFail(serverSql, querySql, exception); } }
/// <summary> /// A method called when connecting to the database server completed successfully. /// </summary> /// <param name="server">The database server.</param> protected override void OnConnectSucceeded(DbServer server) { // Enable the control. this.tabControl.Enabled = true; // Raise the database operation finished event. if (this.DatabaseOperationFinished != null) this.DatabaseOperationFinished(this, EventArgs.Empty); }
// Protected methods. /// <summary> /// An event handler called when a new database server has been set. /// </summary> /// <param name="oldServer">The old server.</param> /// <param name="newServer">The new server.</param> protected virtual void OnServerSet(DbServer oldServer, DbServer newServer) { // If the server has not changed, do nothing. if (oldServer == newServer) return; if (newServer == null) { this.labelTitle.Text = "No server selected"; this.tabControl.Visible = false; } else { this.labelTitle.Text = newServer.Name; this.textBoxName.Text = newServer.Name; this.textBoxId.Text = newServer.Id.ToString(); this.textBoxClass.Text = newServer.ClassName; this.textBoxType.Text = newServer.TypeName; this.textBoxDataSource.Text = newServer.DataSource; this.textBoxUsername.Text = newServer.Username; this.textBoxPassword.SecureText = newServer.Password; this.textBoxDateCreated.Text = newServer.DateCreated.ToString(); this.textBoxDateModified.Text = newServer.DateModified.ToString(); this.pictureBox.Image = ControlServerProperties.images[(int)newServer.State]; this.tabControl.Visible = true; } this.tabControl.SelectedTab = this.tabPageGeneral; if (this.Focused) { this.textBoxName.Select(); this.textBoxName.SelectionStart = 0; this.textBoxName.SelectionLength = 0; } }
/// <summary> /// Executes a database query. /// </summary> /// <param name="server">The database server.</param> /// <param name="query">The database query.</param> protected abstract void DatabaseQuery(DbServer server, DbQuery query);