设为首页 加入收藏

TOP

巧用C# Split()函数获取SQL语句中操作字段(二)
2014-11-23 17:56:09 来源: 作者: 【 】 浏览:78
Tags:巧用 Split 函数 获取 SQL 语句 操作
m().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);


很简单,有待完善。


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Ember.js创建简单的Todo应用 下一篇C#之自定义的implicit和explicit..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: