歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 巧用C# Split()函數獲取SQL語句中操作字段

巧用C# Split()函數獲取SQL語句中操作字段

日期:2017/3/1 9:37:48   编辑:Linux編程

這是前天工作時要求的,將SQL語句的操作字段獲取出來掛在樹節點上,感覺這個函數以後還有可能會用到,特此總結一下,函數中沒有實現Select *的操作,只要添加判斷條件即可。

工具函數:Split()函數:通過字符分割字符串為一個string類型的一維數組。

String.Split 方法有6個重載函數:
1) public string[] Split(params char[] separator) 返回的字符串數組包含此實例中的子字符串
2) public string[] Split(char[] separator, int count) 返回的字符串數組包含此實例中的子字符串。 參數指定返回的子字符串的最大個數。
3) public string[] Split(char[] separator, StringSplitOptions options) 返回的字符串數組包含此字符串中的子字符串。 參數指定是否返回空數組元素。
4) public string[] Split(string[] separator, StringSplitOptions options) 返回的字符串數組包含此字符串中的子字符串。 參數指定是否返回空數組元素。
5) public string[] Split(char[] separator, int count, StringSplitOptions options) 返回的字符串數組包含此字符串中的子字符串(由指定 Unicode 字符數組的元素分隔)。 參數指定要返回子字符串的最大數量,以及是否返回空數組元素。
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
返回的字符串數組包含此字符串中的子字符串(由指定字符串數組的元素分隔)。 參數指定要返回子字符串的最大數量,以及是否返回空數組元素。

注:
StringSplitOptions 為一個枚舉類型,有兩個成員:(1)None:返回值包括含有空字符串的數組元素;(2)RemoveEmptyEntries:返回值不包括含有空字符串的數組元素 。具體例子可到網上找相關代碼。
思路:將字符串通過Split()函數進行分割為字符串數組,然後對字符串數據進行判斷即可。

效果:

代碼:

/// <summary>
/// 獲取查詢字段代碼
/// </summary>
/// <param name="sOperationSQL"></param>
/// <returns></returns>
private string GetSQLOperateField(string sOperationSQL)
{
if (string.IsNullOrEmpty(sOperationSQL))
{
return string.Empty;
}

string sAppendNodeText = string.Empty;
sOperationSQL = sOperationSQL.Trim();
string[] strList = null;
string sKey = sOperationSQL.Substring(0, 6);
switch (sKey.ToUpper())
{
case "SELECT":
{
strList = sOperationSQL.Split(new char[2] { ' ', ',' });
for (int i = 1; i < strList.Length; i++)
{
if (strList[i].ToUpper().Equals("FROM"))
{
break;
}

if (!string.IsNullOrEmpty(strList[i]))
{
sAppendNodeText += strList[i] + ",";
}
}

if (sAppendNodeText.Length > 1)
{
sAppendNodeText = sAppendNodeText.Substring(0, sAppendNodeText.Length - 1);
}
sAppendNodeText = "SELECT【" + sAppendNodeText + "】";
break;
}
case "INSERT":
{
strList = sOperationSQL.Trim().Split(new char[5] { ' ', ',', '(', ')', '\'' });
for (int i = 0; i < strList.Length; i++)
{
if (strList[i].ToUpper().Equals("WHERE"))
{
break;
}

if (!string.IsNullOrEmpty(strList[i]) && strList[i].IndexOf("=") > -1 && strList[i].Length > 1)
{
int iIndex = strList[i].IndexOf("=");
if (iIndex > 0)
{
sAppendNodeText += strList[i].Substring(0, iIndex) + ",";
}
}

if (!string.IsNullOrEmpty(strList[i]) && strList[i].IndexOf("=", 0, 1) > -1)
{
sAppendNodeText += strList[i - 1] + ",";
}
}

if (sAppendNodeText.Length > 1)
{
sAppendNodeText = sAppendNodeText.Substring(0, sAppendNodeText.Length - 1);
}

sAppendNodeText = "INSERT【" + sAppendNodeText + "】";
break;
}
case "UPDATE":
{
strList = sOperationSQL.Trim().Split(new char[2] { ' ', ',' });
for (int i = 0; i < strList.Length; i++)
{
if (strList[i].ToUpper().Equals("WHERE"))
{
break;
}

if (!string.IsNullOrEmpty(strList[i]) && strList[i].IndexOf("=") > -1 && strList[i].Length > 1)
{
int iIndex = strList[i].IndexOf("=");
if (iIndex > 0)
{
sAppendNodeText += strList[i].Substring(0, iIndex) + ",";
}
}

if (!string.IsNullOrEmpty(strList[i]) && strList[i].IndexOf("=", 0, 1) > -1)
{
sAppendNodeText += strList[i - 1] + ",";
}
}

if (sAppendNodeText.Length > 1)
{
sAppendNodeText = sAppendNodeText.Substring(0, sAppendNodeText.Length - 1);
}

sAppendNodeText = "UPDATE【" + sAppendNodeText + "】";
break;
}
case "DELETE":
{
strList = sOperationSQL.Trim().Split(new char[2] { ' ', ',' });
for (int i = 0; i < strList.Length; i++)
{
if (!string.IsNullOrEmpty(strList[i]) && strList[i].IndexOf("=") > -1 && strList[i].Length > 1)
{
int iIndex = strList[i].IndexOf("=");
if (iIndex > 0)
{
sAppendNodeText += strList[i].Substring(0, iIndex) + ",";
}
}

if (!string.IsNullOrEmpty(strList[i]) && strList[i].IndexOf("=", 0, 1) > -1)
{
sAppendNodeText += strList[i - 1] + ",";
}
}

if (sAppendNodeText.Length > 1)
{
sAppendNodeText = sAppendNodeText.Substring(0, sAppendNodeText.Length - 1);
}

sAppendNodeText = "DELETE【" + sAppendNodeText + "】";
break;
}
}

return sAppendNodeText;
}

測試用例:

string sSelSQL = "SELECT BSM FROM STUDENT";
string sResult = GetSQLOperateField(sSelSQL);
this.treeList1.AppendNode(new object[] { sResult }, null);
string sUpdata = "UPDATE STUDENT SET NAME='JAMES'";
sResult = GetSQLOperateField(sUpdata);
this.treeList1.AppendNode(new object[] { sResult }, null);
string sInsertSQL = "INSERT INTO STUDENT NAME='LUCY',ID='2001' WHERE SCORE='95'";
//string sInsertSQL = "INSERT INTO Persons (LastName, Address) VALUES ('Wilson', 'Champs-Elysees')"; //暫不支持這種寫法
sResult = GetSQLOperateField(sInsertSQL);
this.treeList1.AppendNode(new object[] { sResult }, null);

很簡單,有待完善。

C#多線程編程實例 線程與窗體交互【附源碼】 http://www.linuxidc.com/Linux/2014-07/104294.htm

C#數學運算表達式解釋器 http://www.linuxidc.com/Linux/2014-07/104289.htm

在C語言中解析JSON配置文件 http://www.linuxidc.com/Linux/2014-05/101822.htm

C++ Primer Plus 第6版 中文版 清晰有書簽PDF+源代碼 http://www.linuxidc.com/Linux/2014-05/101227.htm

Copyright © Linux教程網 All Rights Reserved