欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實現(xiàn)格式化SQL語句的示例代碼

 更新時間:2023年08月23日 10:32:05   作者:Csharp?小記  
這篇文章主要為大家詳細介紹了C#如何實現(xiàn)格式化SQL語句的功能,文中的示例代碼簡潔易懂,具有一定的借鑒價值,感興趣的小伙伴可以跟隨小編一起學習一下

前言

本來想自己去實現(xiàn)這個功能的,后來吧,經過一番深思熟慮,我覺得還是太麻煩了。所以,打開Github,搜索現(xiàn)成的輪子。。。

即便是在GIithub上,關于用c#來做這個功能的我也沒找到幾個(也可能是我搜索姿勢不太標準)。

說實話,里面的代碼我也沒有去研究,但大致就是把SQL語句經過一系列分析后,轉換成HTM文件,然后用WebBrowser加載出來??雌饋磉€是比較簡單的,但是作者寫的比較復雜,使我喪失了仔細研讀的耐心。。。

不過基于上面這種思想的話,我倒是覺得大可不必非要找C#語言的了,也可以同步看下js的,反正最后是加載html,直接拿過來用可能也比較簡單。

原作者的這個工具寫的比較完善,v2版本用了dev控件做了美化,而且是實時(定時)檢測sql語句進行格式化。如下:

我就做的比較簡單了,頁面還是仿照了原作者的布局,但是去掉了一些代碼,為了更簡單的調用。。。

可以直接把BaiSqlFormatLib.dll拿來調用,或者集成源代碼到項目中。

實現(xiàn)功能

輸入SQL語句進行格式化

開發(fā)環(huán)境

開發(fā)工具:Visual Studio 2013

.NET Framework版本:4.5

實現(xiàn)代碼

public Form1()
        {
            InitializeComponent();
            Init();
        }
        #region 初始化配置
        ISqlTokenizer _tokenizer;
        ISqlTokenParser _parser;
        ISqlTreeFormatter _formatter;
        public void Init()
        {
            chk_default.Checked = true;
            _tokenizer = new BaiSqlFormatLib.Tokenizers.TSqlStandardTokenizer();
            _parser = new BaiSqlFormatLib.Parsers.TSqlStandardParser();
        }
        #endregion
        #region 格式化
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.F6)
            {
                txt_format.DocumentText = Format(txt_sql.Text);
            }
        }
        private string Format(string inputSql)
        {
            SetFormatter();
            var tokenizedSql = _tokenizer.TokenizeSQL(inputSql, inputSql.Length);
            var parsedSql = _parser.ParseSQL(tokenizedSql);
            string subSqlHtml = _formatter.FormatSQLTree(parsedSql);
            return subSqlHtml;
        }
        /// <summary>
        /// 設置格式化屬性
        /// </summary>
        private void SetFormatter()
        {
            ISqlTreeFormatter innerFormatter = new BaiSqlFormatLib.Formatters.TSqlStandardFormatter(new BaiSqlFormatLib.Formatters.TSqlStandardFormatterOptions
            {
                IndentString = "\\s\\s\\s\\s", //縮進內容
                SpacesPerTab = 4,
                MaxLineWidth = Convert.ToInt32(txt_maxWidth.Text), //單行字符串最大長度
                ExpandCommaLists = !chk_columnNotNewline.Checked,  //false 字段換行 
                KeywordAlign = chk_keywordAlign.Checked,     //字段對齊
                TrailingCommas = true,   //true 逗號在字段之后
                SpaceAfterExpandedComma = true,  //true 逗號后追加空格
                ExpandBooleanExpressions = chk_conditionNewline.Checked,  //true 條件換行
                ExpandCaseStatements = chk_expandCase.Checked,      //true case when換行
                ExpandBetweenConditions = chk_expandBetween.Checked,  //true between 換行
                ExpandInLists = chk_expandIn.Checked,   //true in 內容換行
                BreakJoinOnSections = chk_expandOn.Checked, //true join on中on 條件換行
                UppercaseKeywords = chk_uppercaseKeywords.Checked, //true 關鍵字大寫
                AllUpper = chk_allUpper.Checked, //true 全部大寫
                HTMLColoring = chk_coloring.Checked, //true HTML顏色標記 默認為true
                KeywordStandardization = true,//true 關鍵字標準化
                NewStatementLineBreaks = 2, //新語句換行數(shù)
                NewClauseLineBreaks = 1,//遇到關鍵字 換行數(shù)
                AllIndent = chk_allIndent.Checked, //整體縮進一個IndentString
                AsAlign = chk_asAlign.Checked, //true as對齊
                KeywordLengthOfAs = Convert.ToInt32(txt_asMaxWidth.Text)//as字段的最大長度
            });
            _formatter = new BaiSqlFormatLib.Formatters.HtmlPageWrapper(innerFormatter);
        }
        #endregion
        #region 頁面配置
        private void chk_default_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_default.Checked)
            {
                chk_custom.CheckState = CheckState.Unchecked;
                txt_maxWidth.Text = "170";
                chk_columnNotNewline.CheckState = CheckState.Unchecked;
                chk_keywordAlign.CheckState = CheckState.Checked;
                chk_conditionNewline.CheckState = CheckState.Checked;
                chk_expandCase.CheckState = CheckState.Checked;
                chk_expandBetween.CheckState = CheckState.Unchecked;
                chk_expandIn.CheckState = CheckState.Unchecked;
                chk_expandOn.CheckState = CheckState.Checked;
                chk_uppercaseKeywords.CheckState = CheckState.Unchecked;
                chk_allUpper.CheckState = CheckState.Unchecked;
                chk_coloring.CheckState = CheckState.Checked;
                chk_allIndent.CheckState = CheckState.Checked;
                chk_asAlign.CheckState = CheckState.Checked;
                txt_asMaxWidth.Text = "35";
                chk_addSemicolon.CheckState = CheckState.Checked;
                txt_maxWidth.Enabled = false;
                chk_columnNotNewline.Enabled = false;
                chk_keywordAlign.Enabled = false;
                chk_conditionNewline.Enabled = false;
                chk_expandCase.Enabled = false;
                chk_expandBetween.Enabled = false;
                chk_expandIn.Enabled = false;
                chk_expandOn.Enabled = false;
                chk_uppercaseKeywords.Enabled = false;
                chk_allUpper.Enabled = false;
                chk_coloring.Enabled = false;
                chk_allIndent.Enabled = false;
                chk_asAlign.Enabled = false;
                txt_asMaxWidth.Enabled = false;
                chk_addSemicolon.Enabled = false;
            }
            else
                chk_custom.CheckState = CheckState.Checked;
        }
        private void chk_custom_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_custom.Checked)
            {
                chk_default.CheckState = CheckState.Unchecked;
                chk_columnNotNewline.Enabled = true;
                chk_keywordAlign.Enabled = true;
                chk_conditionNewline.Enabled = true;
                chk_expandCase.Enabled = true;
                chk_expandBetween.Enabled = true;
                chk_expandIn.Enabled = true;
                chk_expandOn.Enabled = true;
                chk_uppercaseKeywords.Enabled = true;
                chk_allUpper.Enabled = true;
                chk_coloring.Enabled = true;
                chk_allIndent.Enabled = true;
                chk_asAlign.Enabled = true;
                chk_addSemicolon.Enabled = true;
                txt_maxWidth.Enabled = true;
                txt_asMaxWidth.Enabled = true;
            }
            else
                chk_default.CheckState = CheckState.Checked;
        }
        #endregion

實現(xiàn)效果

到此這篇關于C#實現(xiàn)格式化SQL語句的示例代碼的文章就介紹到這了,更多相關C#格式化SQL語句內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論