这是前天工作时要求的,将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)
返回的字符串数组包含此字符串中的子字符串(由指定字符串数组的元素分隔)。 参数指定要返回子字符串的最大数量,以及是否返回空数组元素。
注:
代码:
/// <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);
很简单,有待完善。