这是前天工作时要求的,将SQL语句的操作字段获取出来挂在树节点上,感觉这个函数以后还有可能会用到,特此总结一下,函数中没有实现Select *的操作,只要添加判断条件即可。
工具函数:Split()函数:通过字符分割字符串为一个string类型的一维数组。
/// <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);原文地址:http://blog.csdn.net/anlidengshiwei/article/details/40817245