There are two ways to work with the third version of Active Query Builder. The simple way that is suitable for majority of users is provided below.

The other way might be needed if you want to re-organize the controls inside the QueryBuilder object in unusual way (for example, to place the Design Area and the Query Column List on separate tabs, etc.), to place the Database Schema Tree on a separate form, or to work with Active Query Builder in non-visual way (for SQL query parsing, analysis or modification). You can read about it here.

There are two or three components you need to use to get started with Active Query Builder: a syntax provider that suits your database server, a metadata provider that suits your DB connection and the QueryBuilder component that incapsulates all visual controls in a single visual control and has everything that user needs to build a query.

- QueryBuilder is a main visual component. It unites all visual controls among with SQL query building logic controllers, SQL query object model, SQL parsing and generation functionality in a single object.

- Metadata Provider is a non-visual object to get connected to database and extract the needed information about database schema. There are different metadata providers for different .NET DB providers. This object is only needed if you work with live database connection.

- Syntax Provider is a non-visual object that determines syntax rules to build correct queries for different SQL dialects. There are lots of supported SQL syntaxes for most popular database servers, as well as for generic ANSI-compatible SQL language. This object is necessary.

You can read more about Metadata and Syntax Providers here.

Follow the steps below to add Active Query Builder to your application.

  1. Place the QueryBuilder component on the form.
    using ActiveQueryBuilder.Core;
    using ActiveQueryBuilder.View.WPF; // or ActiveQueryBuilder.View.WinForms for WinForms Edition
    // ...
    
    QueryBuilder queryBuilder1 = new QueryBuilder();
  2. Place the needed metadata and syntax provider components on the form or create them by code. Define a proper database connection object for the metadata provider.
    OleDbConnection connection = new OleDbConnection();
    connection.ConnectionString = "<your connection string here>";
    
    OLEDBMetadataProvider metadataProvider = new OLEDBMetadataProvider();
    metadataProvider.Connection = connection;
    
    GenericSyntaxProvider syntaxProvider = new GenericSyntaxProvider();
    Connection string examples can be found here.
  3. Link the components above to the QueryBuilder by setting MetadataProvider and SyntaxProvider properties
    queryBuilder1.MetadataProvider = metadataProvider;
    queryBuilder1.SyntaxProvider = syntaxProvider;
  4. Add SQLTextEditor, TextBox, or any other text editing control to a form if you want to allow for direct SQL text editing by end-users.
  5. Establish a connection between the QueryBuilder and the TextBox components.
    Set the following code for the Validate event of TextBox component:
    private void textBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
    {
        try
        {
    
            // Update the query builder with manually edited query text:
            queryBuilder1.SQL = textBox1.Text;
    
        }
        catch (SQLParsingException ex)
        {
            e.Cancel = true;
    
            // Set caret to error position
            textBox1.SelectionStart = ex.ErrorPos.pos;
    
            // Report error
            MessageBox.Show(ex.Message, "Parsing error");
        }
    }
    Add the following handler for the QueryBuilder.SQLUpdated event:
    textBox1.Text = queryBuilder1.FormattedSQL;
  6. Call the following method to activate Active Query Builder's database schema tree:
    queryBuilder1.InitializeDatabaseSchemaTree();
  7. That's all! Now you can run your application.

Learn how to work with Active Query Builder in non-visual mode or to split QueryBuilder to separate visual controls.

(!) Quick Start Guide #WinForms #WPF

Is this article helpful for you?