{"id":278,"date":"2010-07-20T02:34:55","date_gmt":"2010-07-20T07:34:55","guid":{"rendered":"http:\/\/www.dpriver.com\/blog\/?page_id=278"},"modified":"2011-04-11T01:23:22","modified_gmt":"2011-04-11T06:23:22","slug":"sql-beautifier-format-sql-in-your-own-program","status":"publish","type":"page","link":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/sql-beautifier-format-sql-in-your-own-program\/","title":{"rendered":"SQL beautifier, format SQL in your own program"},"content":{"rendered":"

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

pseudo code in C#:<\/p>\n

    \r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.ComponentModel;\r\nusing System.Data;\r\nusing System.Drawing;\r\nusing System.Text;\r\nusing System.Windows.Forms;\r\n\r\nusing gudusoft.gsqlparser;\r\nusing gudusoft.gsqlparser.Units;\r\n\r\nnamespace formatsql\r\n{\r\n    public partial class Form1 : Form\r\n    {\r\n        public Form1()\r\n        {\r\n            InitializeComponent();\r\n        }\r\n\r\n        private void setupformatoptions()\r\n        {\r\n            \/\/for more format options, please check document\r\n\r\n            lzbasetype.gFmtOpt.Select_Columnlist_Style = TAlignStyle.asStacked;\r\n            lzbasetype.gFmtOpt.Select_Columnlist_Comma = TLinefeedsCommaOption.lfAfterComma;\r\n            lzbasetype.gFmtOpt.SelectItemInNewLine = false;\r\n            lzbasetype.gFmtOpt.AlignAliasInSelectList = true;\r\n            lzbasetype.gFmtOpt.TreatDistinctAsVirtualColumn = false;\r\n\r\n            \/\/setup more format options ...\r\n            \r\n            lzbasetype.gFmtOpt.linenumber_enabled = true;\r\n        }\r\n\r\n        private void setuphighlighterAttributes()\r\n        {\r\n            lzbasetype.gFmtOpt.HighlightingFontname = \"Courier New\";\r\n            lzbasetype.gFmtOpt.HighlightingFontsize = 10;\r\n\r\n            \/\/for other elements you want to customize, please check document\r\n            lzbasetype.gFmtOpt.HighlightingElements[(int)TLzHighlightingElement.sfkIdentifer].SetForegroundInRGB(\"#008000\");\r\n            lzbasetype.gFmtOpt.HighlightingElements[(int)TLzHighlightingElement.sfkIdentifer].StyleBold = true;\r\n            lzbasetype.gFmtOpt.HighlightingElements[(int)TLzHighlightingElement.sfkIdentifer].StyleItalic = false;\r\n            lzbasetype.gFmtOpt.HighlightingElements[(int)TLzHighlightingElement.sfkIdentifer].StyleStrikeout = false;\r\n            lzbasetype.gFmtOpt.HighlightingElements[(int)TLzHighlightingElement.sfkIdentifer].StyleUnderline = false;\r\n\r\n            \/\/setup more elements attributes ....\r\n        }\r\n\r\n\r\n        private void btnFormatSQL_Click(object sender, EventArgs e)\r\n        {\r\n            tabControl1.SelectedIndex = 0;\r\n            setupformatoptions();\r\n            \r\n            TGSqlParser sqlparser = new TGSqlParser(TDbVendor.DbVMssql);\r\n            sqlparser.SqlText.Text = inputsql.Text;\r\n            int i = sqlparser.PrettyPrint();\r\n            if (i == 0)\r\n            {\r\n                outputsql.Text = sqlparser.FormattedSqlText.Text;\r\n            }\r\n            else\r\n            {\r\n                outputsql.Text = sqlparser.ErrorMessages;\r\n            }\r\n        }\r\n\r\n        private void btnFormatSqlToHtml_Click(object sender, EventArgs e)\r\n        {\r\n            tabControl1.SelectedIndex = 1;\r\n            setupformatoptions();\r\n            setuphighlighterAttributes();\r\n\r\n            TGSqlParser sqlparser = new TGSqlParser(TDbVendor.DbVMssql);\r\n            sqlparser.SqlText.Text = inputsql.Text;\r\n            webBrowser1.DocumentText = sqlparser.ToHtml(TOutputFmt.ofhtml);\r\n\r\n        }\r\n\r\n        private void btnFormatSQLInRTF_Click(object sender, EventArgs e)\r\n        {\r\n            tabControl1.SelectedIndex = 2;\r\n            setupformatoptions();\r\n            setuphighlighterAttributes();\r\n\r\n            TGSqlParser sqlparser = new TGSqlParser(TDbVendor.DbVMssql);\r\n            sqlparser.SqlText.Text = inputsql.Text;\r\n            richTextBox1.Rtf = sqlparser.ToRTF(TOutputFmt.ofrtf);\r\n\r\n        }\r\n    }\r\n}\r\n<\/pre>\nDownload this demo: SQL formatter with source code<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"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\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":235,"menu_order":0,"comment_status":"open","ping_status":"open","template":"gsp_feature_page_tt.php","meta":[],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":5}},"yoast_head":"\nHelp you to make better use of General SQL Parser<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n