Skip to content
No results
We use cookies to ensure that we give you the best experience on our website.

# SQL beautifier, format SQL in your own program

Add SQL format/beautify features in your own program in 5 minutes, 10 lines code with more than 80 format options. Formatted SQL query can be in plain text, or colored, syntax highlight in RTF or HTML.

pseudo code in C#:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    using gudusoft.gsqlparser;
    using gudusoft.gsqlparser.Units;

    namespace formatsql
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void setupformatoptions()
            {
                //for more format options, please check document

                lzbasetype.gFmtOpt.Select_Columnlist_Style = TAlignStyle.asStacked;
                lzbasetype.gFmtOpt.Select_Columnlist_Comma = TLinefeedsCommaOption.lfAfterComma;
                lzbasetype.gFmtOpt.SelectItemInNewLine = false;
                lzbasetype.gFmtOpt.AlignAliasInSelectList = true;
                lzbasetype.gFmtOpt.TreatDistinctAsVirtualColumn = false;

                //setup more format options ...

                lzbasetype.gFmtOpt.linenumber_enabled = true;
            }

            private void setuphighlighterAttributes()
            {
                lzbasetype.gFmtOpt.HighlightingFontname = "Courier New";
                lzbasetype.gFmtOpt.HighlightingFontsize = 10;

                //for other elements you want to customize, please check document
                lzbasetype.gFmtOpt.HighlightingElements[(int)TLzHighlightingElement.sfkIdentifer].SetForegroundInRGB("#008000");
                lzbasetype.gFmtOpt.HighlightingElements[(int)TLzHighlightingElement.sfkIdentifer].StyleBold = true;
                lzbasetype.gFmtOpt.HighlightingElements[(int)TLzHighlightingElement.sfkIdentifer].StyleItalic = false;
                lzbasetype.gFmtOpt.HighlightingElements[(int)TLzHighlightingElement.sfkIdentifer].StyleStrikeout = false;
                lzbasetype.gFmtOpt.HighlightingElements[(int)TLzHighlightingElement.sfkIdentifer].StyleUnderline = false;

                //setup more elements attributes ....
            }

            private void btnFormatSQL_Click(object sender, EventArgs e)
            {
                tabControl1.SelectedIndex = 0;
                setupformatoptions();

                TGSqlParser sqlparser = new TGSqlParser(TDbVendor.DbVMssql);
                sqlparser.SqlText.Text = inputsql.Text;
                int i = sqlparser.PrettyPrint();
                if (i == 0)
                {
                    outputsql.Text = sqlparser.FormattedSqlText.Text;
                }
                else
                {
                    outputsql.Text = sqlparser.ErrorMessages;
                }
            }

            private void btnFormatSqlToHtml_Click(object sender, EventArgs e)
            {
                tabControl1.SelectedIndex = 1;
                setupformatoptions();
                setuphighlighterAttributes();

                TGSqlParser sqlparser = new TGSqlParser(TDbVendor.DbVMssql);
                sqlparser.SqlText.Text = inputsql.Text;
                webBrowser1.DocumentText = sqlparser.ToHtml(TOutputFmt.ofhtml);

            }

            private void btnFormatSQLInRTF_Click(object sender, EventArgs e)
            {
                tabControl1.SelectedIndex = 2;
                setupformatoptions();
                setuphighlighterAttributes();

                TGSqlParser sqlparser = new TGSqlParser(TDbVendor.DbVMssql);
                sqlparser.SqlText.Text = inputsql.Text;
                richTextBox1.Rtf = sqlparser.ToRTF(TOutputFmt.ofrtf);

            }
        }
    }

Download this demo: SQL formatter with source code

## Leave a Reply

Your email address will not be published. Required fields are marked *

Name *

Email *

Website

Add Comment

Save my name, email, and website in this browser for the next time I comment.

I accept the Privacy Policy