How to get formatted SQL text and setup formatting options?
Last modified:
Active Query Builder for .NET 3.0
The standard way of getting SQL query text
Using the QueryBuilder visual control, you can set up SQL text formatting using the QueryBuilder.SQLFormattingOptions property group and read SQL text from the QueryBuilder.FormattedSQL property.
An alternate way of getting SQL query text
The SQLBuilder or FormattedSQLBuilder objects must be used when you need to get SQL query text. The GetSQL static method of these objects accepts the SubQuery and SQLGenerationOptions/SQLFormattingOptions objects and returns SQL text in the result. The SQLFormattingOptions class is inherited from the SQLGenerationOptions class. Both are used to store SQL generation options, such as the need for a quotation of all identifiers in query text, the need for fully qualified notation of database object and field names, etc. The SQLFormattingOptions object additionally holds properties to tune the formatting of SQL query text (line breaks, indents, word wrapping).
// copy the current formatting options and redefine just some of them using (var options = new SQLFormattingOptions(queryBuilder1.SQLFormattingOptions) { // initialize some props here UseAltNames = false, }) { sqlTextEditor1.Text = queryBuilder1.GetSQL(options); } // another way sqlTextEditor1.Text = FormattedSQLBuilder.GetSQL(sqlQuery1.QueryRoot, sqlFormattingOptions1);
Other editions
In version 2.x and lower, to get the formatted SQL text, you should create an instance of the PlainTextSQLBuilder object and assign a QueryBuilder object to the PlainTextSQLBuilder.QueryBuilder property. After that, you can handle the PlainTextSQLBuilder.SQLUpdated event to get the formatted SQL text.
The links below point to the lists of available properties of AQB.NET 2.0 to set up the formatting:
PlainTextSQLBuilder inherits BaseSQLBuilder which has separate sets of properties for the Main query, expression subqueries, derived tables, and Common Table Expressions.
A set of formatting options for a single query is represented by the SQLBuilderSelectFormat class.