/// <summary> /// Cancels the specified command. /// </summary> /// <param name="command">The database command to cancel.</param> protected void DatabaseQueryCancel(DbCommand command) { // Cancel the command. command.Cancel(); // Call the event handler. this.OnQueryCanceling(command.Query); }
// Protected methods. /// <summary> /// A method called when the execution of the database query starts. /// </summary> /// <param name="server">The database server.</param> /// <param name="query">The database query.</param> /// <param name="command">The database command.</param> protected virtual void OnQueryStarted(DbServerSql server, DbQuerySql query, DbCommand command) { }
/// <summary> /// A method called when staring a query. /// </summary> /// <param name="server">The database server.</param> /// <param name="query">The database query.</param> /// <param name="command">The database command.</param> protected override void OnQueryStarted(DbServerSql server, DbQuerySql query, DbCommand command) { // Disable the control. this.tabControl.Enabled = false; // Raise the database operation started event. if (this.DatabaseOperationStarted != null) this.DatabaseOperationStarted(this, EventArgs.Empty); }
/// <summary> /// An event handler called when the refresh operation completed successfully and the resulting data is object /// data. /// </summary> /// <param name="server">The database server.</param> /// <param name="query">The database query.</param> /// <param name="result">The database result.</param> /// <param name="recordsAffected">The number of records affected.</param> protected override void OnQuerySucceeded(DbServerSql server, DbQuerySql query, DbDataObject result, int recordsAffected) { // Set the current result. this.result = result; try { // Add the table rows. for (int row = 0; row < this.result.RowCount; row++) { // Add the new row, and get the row index. int index = this.dataGrid.Rows.Add(this.query.Table.GetValues(this.result[row])); // Set the row tag with the table object. this.dataGrid.Rows[index].Tag = result[row]; } } catch (Exception exception) { // If an exception occurs, call the refresh fail method. this.OnQueryFailed(server, query, exception); return; } // Set the current command to null. this.command = null; // Enable the buttons. this.buttonRefresh.Enabled = true; this.buttonCancel.Enabled = false; this.buttonClose.Enabled = true; // Update the status box. this.labelStatus.Text = "{0} object{1} fetched.".FormatWith(result.RowCount, result.RowCount.PluralSuffix()); // Raise the database operation finished event. if (this.DatabaseOperationFinished != null) this.DatabaseOperationFinished(this, EventArgs.Empty); }
/// <summary> /// A method called when the execution of the database query starts. /// </summary> /// <param name="server">The database server.</param> /// <param name="query">The database query.</param> /// <param name="command">The database command.</param> protected override void OnQueryStarted(DbServerSql server, DbQuerySql query, DbCommand command) { // Save the current command. this.command = command; // Enable the cancel button. this.buttonRefresh.Enabled = false; this.buttonClose.Enabled = false; this.buttonCancel.Enabled = true; // Raise the database operation started event. if (this.DatabaseOperationStarted != null) this.DatabaseOperationStarted(this, EventArgs.Empty); }
/// <summary> /// A method called when the executiopn of the database query has failed. /// </summary> /// <param name="server">The database server.</param> /// <param name="query">The database query.</param> /// <param name="exception">The exception.</param> protected override void OnQueryFailed(DbServerSql server, DbQuerySql query, Exception exception) { // Enable the buttons. this.buttonRefresh.Enabled = true; this.buttonCancel.Enabled = false; this.buttonClose.Enabled = true; // Set the current command to null. this.command = null; // Update the status box. this.labelStatus.Text = "Query failed. {0}".FormatWith(exception.Message); // Raise the database operation finished event. if (this.DatabaseOperationFinished != null) this.DatabaseOperationFinished(this, EventArgs.Empty); }
/// <summary> /// An event handler called when the user clicks on the start query button. /// </summary> /// <param name="sender">The sender object.</param> /// <param name="e">The event arguments.</param> private void OnStart(object sender, EventArgs e) { // Execute the code on the UI thread. this.Invoke(() => { // If the database server is not connected, first connect to the database. if (this.server.State != DbServerSql.ServerState.Connected) { // Show a connecting message. this.ShowMessage(Resources.Connect_48, "Database", "Connecting to the database server \'{0}\'...".FormatWith(this.server.Name)); try { // Connect asynchronously to the database server, and add this method as a handler. this.server.Open(this.OnConnected, new EventHandler(this.OnStart)); } catch (Exception exception) { // If an exception occurs, hide the connecting message. this.HideMessage(); // Display an error message box to the user. MessageBox.Show( this, "Connecting to the database server \'{0}\' failed. {1}".FormatWith(this.server.Name, exception.Message), "Connecting to Database Failed", MessageBoxButtons.OK, MessageBoxIcon.Error ); } return; } // Refresh the list of databases. // Create a new database command for the list of server databases. try { // Clear the result table. this.dataGrid.Rows.Clear(); this.dataGrid.Columns.Clear(); // Disable the start button. this.buttonStart.Enabled = false; // Enable the stop button. this.buttonStop.Enabled = true; // Disable the query code box. this.codeBox.Enabled = false; // Set the command to null. this.command = null; // Show a connecting message. this.ShowMessage(Resources.DatabaseBusy_48, "Database", "Executing query on the database server \'{0}\'...".FormatWith(this.server.Name)); // Create the command. this.command = this.server.CreateCommand(DbQuerySql.Create(this.codeBox.Text)); // Execute the command. command.ExecuteReader((DbAsyncResult commandResult, DbReader reader) => { try { // Throw the command exception, if any. if (commandResult.Exception != null) throw commandResult.Exception; // Read the results table. reader.Read(null, (DbAsyncResult readerResult, DbDataRaw result) => { try { // Throw the reader exception, if any. if (readerResult.Exception != null) { reader.Close(); throw readerResult.Exception; } // Get the number of records affected. int recordsAffected = reader.RecordsAffected; // Close the reader. reader.Close(); // Dispose and reset the command. this.command.Dispose(); this.command = null; // Show a success message. this.ShowMessage(Resources.DatabaseSuccess_48, "Database", "Executing query on the database server \'{0}\' completed successfully.".FormatWith(this.server.Name), false); // Wait. Thread.Sleep(ApplicationConfig.MessageCloseDelay); // Hide the message. this.HideMessage(); // Call the completion method. this.OnQuerySuccess(result, recordsAffected); } catch (Exception exception) { // Dispose and reset the command. this.command.Dispose(); this.command = null; // Show an error message. this.ShowMessage(Resources.DatabaseError_48, "Database", "Executing query on the database server \'{0}\' failed.".FormatWith(this.server.Name), false); // Log the event. this.server.LogEvent( LogEventLevel.Important, LogEventType.Error, "Executing query on the database server \'{0}\' failed. {1}", new object[] { this.server.Name, exception.Message }, exception); // Wait. Thread.Sleep(ApplicationConfig.MessageCloseDelay); // Hide the message. this.HideMessage(); // Call the completion method. this.OnQueryFail(exception); } }); } catch (Exception exception) { // Dispose and reset the command. this.command.Dispose(); this.command = null; // Show an error message. this.ShowMessage(Resources.DatabaseError_48, "Database", "Executing query on the database server \'{0}\' failed.".FormatWith(this.server.Name), false); // Log the event. this.server.LogEvent( LogEventLevel.Important, LogEventType.Error, "Executing query on the database server \'{0}\' failed. {1}", new object[] { this.server.Name, exception.Message }, exception); // Wait. Thread.Sleep(ApplicationConfig.MessageCloseDelay); // Hide the message. this.HideMessage(); // Call the completion method. this.OnQueryFail(exception); } }); } catch (Exception exception) { // Dispose and reset the command. this.command.Dispose(); this.command = null; // Show an error message. this.ShowMessage(Resources.DatabaseError_48, "Database", "Executing query on the database server \'{0}\' failed.".FormatWith(this.server.Name), false); // Log the event. this.server.LogEvent( LogEventLevel.Important, LogEventType.Error, "Executing query on the database server \'{0}\' failed. {1}", new object[] { this.server.Name, exception.Message }, exception); // Wait. Thread.Sleep(ApplicationConfig.MessageCloseDelay); // Hide the message. this.HideMessage(); // Call the completion method. this.OnQueryFail(exception); } }); }