C#實(shí)現(xiàn)格式化SQL語(yǔ)句的示例代碼
前言
本來(lái)想自己去實(shí)現(xiàn)這個(gè)功能的,后來(lái)吧,經(jīng)過(guò)一番深思熟慮,我覺(jué)得還是太麻煩了。所以,打開(kāi)Github,搜索現(xiàn)成的輪子。。。
即便是在GIithub上,關(guān)于用c#來(lái)做這個(gè)功能的我也沒(méi)找到幾個(gè)(也可能是我搜索姿勢(shì)不太標(biāo)準(zhǔn))。
說(shuō)實(shí)話,里面的代碼我也沒(méi)有去研究,但大致就是把SQL語(yǔ)句經(jīng)過(guò)一系列分析后,轉(zhuǎn)換成HTM文件,然后用WebBrowser加載出來(lái)??雌饋?lái)還是比較簡(jiǎn)單的,但是作者寫(xiě)的比較復(fù)雜,使我喪失了仔細(xì)研讀的耐心。。。
不過(guò)基于上面這種思想的話,我倒是覺(jué)得大可不必非要找C#語(yǔ)言的了,也可以同步看下js的,反正最后是加載html,直接拿過(guò)來(lái)用可能也比較簡(jiǎn)單。
原作者的這個(gè)工具寫(xiě)的比較完善,v2版本用了dev控件做了美化,而且是實(shí)時(shí)(定時(shí))檢測(cè)sql語(yǔ)句進(jìn)行格式化。如下:
我就做的比較簡(jiǎn)單了,頁(yè)面還是仿照了原作者的布局,但是去掉了一些代碼,為了更簡(jiǎn)單的調(diào)用。。。
可以直接把BaiSqlFormatLib.dll拿來(lái)調(diào)用,或者集成源代碼到項(xiàng)目中。
實(shí)現(xiàn)功能
輸入SQL語(yǔ)句進(jìn)行格式化
開(kāi)發(fā)環(huán)境
開(kāi)發(fā)工具:Visual Studio 2013
.NET Framework版本:4.5
實(shí)現(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> /// 設(shè)置格式化屬性 /// </summary> private void SetFormatter() { ISqlTreeFormatter innerFormatter = new BaiSqlFormatLib.Formatters.TSqlStandardFormatter(new BaiSqlFormatLib.Formatters.TSqlStandardFormatterOptions { IndentString = "\\s\\s\\s\\s", //縮進(jìn)內(nèi)容 SpacesPerTab = 4, MaxLineWidth = Convert.ToInt32(txt_maxWidth.Text), //單行字符串最大長(zhǎng)度 ExpandCommaLists = !chk_columnNotNewline.Checked, //false 字段換行 KeywordAlign = chk_keywordAlign.Checked, //字段對(duì)齊 TrailingCommas = true, //true 逗號(hào)在字段之后 SpaceAfterExpandedComma = true, //true 逗號(hào)后追加空格 ExpandBooleanExpressions = chk_conditionNewline.Checked, //true 條件換行 ExpandCaseStatements = chk_expandCase.Checked, //true case when換行 ExpandBetweenConditions = chk_expandBetween.Checked, //true between 換行 ExpandInLists = chk_expandIn.Checked, //true in 內(nèi)容換行 BreakJoinOnSections = chk_expandOn.Checked, //true join on中on 條件換行 UppercaseKeywords = chk_uppercaseKeywords.Checked, //true 關(guān)鍵字大寫(xiě) AllUpper = chk_allUpper.Checked, //true 全部大寫(xiě) HTMLColoring = chk_coloring.Checked, //true HTML顏色標(biāo)記 默認(rèn)為true KeywordStandardization = true,//true 關(guān)鍵字標(biāo)準(zhǔn)化 NewStatementLineBreaks = 2, //新語(yǔ)句換行數(shù) NewClauseLineBreaks = 1,//遇到關(guān)鍵字 換行數(shù) AllIndent = chk_allIndent.Checked, //整體縮進(jìn)一個(gè)IndentString AsAlign = chk_asAlign.Checked, //true as對(duì)齊 KeywordLengthOfAs = Convert.ToInt32(txt_asMaxWidth.Text)//as字段的最大長(zhǎng)度 }); _formatter = new BaiSqlFormatLib.Formatters.HtmlPageWrapper(innerFormatter); } #endregion #region 頁(yè)面配置 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
實(shí)現(xiàn)效果
到此這篇關(guān)于C#實(shí)現(xiàn)格式化SQL語(yǔ)句的示例代碼的文章就介紹到這了,更多相關(guān)C#格式化SQL語(yǔ)句內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#編程實(shí)現(xiàn)簡(jiǎn)易圖片瀏覽器的方法
這篇文章主要介紹了C#編程實(shí)現(xiàn)簡(jiǎn)易圖片瀏覽器的方法,涉及C#基于WinForm操作圖片實(shí)現(xiàn)預(yù)覽功能的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11C# 異步多線程入門(mén)到精通之ThreadPool篇
ThreadPool 是 .net 2.0 時(shí)代的產(chǎn)物,有了 Thread 為什么還會(huì)有 ThreadPool 呢?ThreadPool 可以做到限制線程數(shù)量、重用線程2021-11-11Unity3D Shader實(shí)現(xiàn)鏡子效果
這篇文章主要為大家詳細(xì)介紹了Unity3D Shader實(shí)現(xiàn)鏡子效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05C# 關(guān)于LoadLibrary的疑問(wèn)詳解
這篇文章主要介紹了C# 關(guān)于LoadLibrary的疑問(wèn)詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08.net文件上傳時(shí)實(shí)現(xiàn)通過(guò)文件頭確認(rèn)文件類型的方法
這篇文章主要介紹了.net文件上傳時(shí)實(shí)現(xiàn)通過(guò)文件頭確認(rèn)文件類型的方法,很實(shí)用的功能,需要的朋友可以參考下2014-07-07C#實(shí)現(xiàn)文件壓縮與解壓的方法示例【ZIP格式】
這篇文章主要介紹了C#實(shí)現(xiàn)文件壓縮與解壓的方法,結(jié)合具體實(shí)例形式分析了C#針對(duì)文件進(jìn)行zip格式壓縮與解壓縮的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06Winform窗口實(shí)現(xiàn)多顯示屏顯示的2種方法
這篇文章主要介紹了Winform窗口實(shí)現(xiàn)多顯示屏顯示的2種方法,本文直接給出了實(shí)現(xiàn)代碼,并對(duì)其中的一些重要參數(shù)做了解釋,需要的朋友可以參考下2015-06-06C# 創(chuàng)建MDB數(shù)據(jù)庫(kù)、并存放表格數(shù)據(jù)的案例
這篇文章主要介紹了C# 創(chuàng)建MDB數(shù)據(jù)庫(kù)、并存放表格數(shù)據(jù)的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01