求C#的分割SQL语句的方法 上面忘记说了,RemoveSqlComment是删除SQL注释的方法(返回string) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 string[] sqlStrs = longSQLstr.Split(';');随便说下,楼主写的那个SQL不正确,里面是逗号,不是分号; 我不是分隔多个字段的啊,为啥用逗号?我是一个字符串';;;;;;' 作为name字段的值插入的啊。 ArrayList al = new ArrayList();string str = "insert into student([name]) values(';;;;;;');select * from student where [name]=';;;;;;';"int icount=0;int lastCount=0;int signcount=0;for(int i=0;i<str.length;i++){ string a = str[i].ToString(); if(a=="'") { icount++; } if(a==";") { signcount++; } if(a==";"&& icount%2==0) { string strTemp = str.substring(lastCount,i-lastCount); al.add(strTemp) }}if(signcount%2!=0){string strTemps = str.substring(lastCount,str.length-lastCount);al.add(strTemps)} 只接用一个特殊字符作为sql语句的分割(这个特殊字符是在sql中不可能使用到的),然后用split就可以了呀。 我要运行标准的SQL脚本的啊。标准的SQL脚本是以分号分割的。 if(signcount%2!=0)最后这个没看懂。signcount这个变量干吗用的?分号的个数?为什么分号的个数为奇数的时候才添加剩余的字符串呢? private static string[] SplitSqlCommand(string commandText) { try { List<string> tempList = new List<string>(); int lastPos = 0; bool flag = false; //表示是否在引号内 for (int currentPos = 0; currentPos < commandText.Length; currentPos++) { char ch = commandText[currentPos]; if (ch == '\'') { flag = !flag; } else if (ch == ';' && !flag) { string tempStr = commandText.Substring(lastPos, currentPos - lastPos).Trim(); if (!string.IsNullOrEmpty(tempStr)) { tempList.Add(tempStr); } lastPos = currentPos + 1; } } if (lastPos < commandText.Length) { string lastStr = commandText.Substring(lastPos, commandText.Length - lastPos).Trim(); if (!string.IsNullOrEmpty(lastStr)) { tempList.Add(lastStr); } } return tempList.ToArray(); } catch (Exception){ } }根据#5楼的代码稍作修改。大家帮忙看一下,还有没有问题了。 datagridview值,传到另一个form 找不到指定的模块。 (异常来自 HRESULT:0x8007007E) 如何在C#中定义结构体数组? 为什么同样的参数,记事本可以打开,写字板不行?! 求助c#组件设计(新人) Treeview结构中的checkbox 请教要把下面的表单在C#中做用什么控件好? 我喜欢的女孩要我帮忙做个东西,大家拉我一把呀! 求dotNet自画窗体标题栏的源码 在RichTextBox控件里面怎么让输入光标移到末尾? C# textbox初始化用dataset取数据,然后,更新textbox后,更新数据库,却发现textbox取的是原来的值! 哪里有问题?
string[] sqlStrs = longSQLstr.Split(';');
随便说下,楼主写的那个SQL不正确,里面是逗号,不是分号;
string str = "insert into student([name]) values(';;;;;;');select * from student where [name]=';;;;;;';"int icount=0;
int lastCount=0;
int signcount=0;
for(int i=0;i<str.length;i++)
{
string a = str[i].ToString();
if(a=="'")
{
icount++;
}
if(a==";")
{
signcount++;
}
if(a==";"&& icount%2==0)
{
string strTemp = str.substring(lastCount,i-lastCount);
al.add(strTemp)
}
}
if(signcount%2!=0)
{string strTemps = str.substring(lastCount,str.length-lastCount);
al.add(strTemps)
}
if(signcount%2!=0)最后这个没看懂。signcount这个变量干吗用的?分号的个数?为什么分号的个数为奇数的时候才添加剩余的字符串呢?
{
try
{
List<string> tempList = new List<string>();
int lastPos = 0;
bool flag = false; //表示是否在引号内
for (int currentPos = 0; currentPos < commandText.Length; currentPos++)
{
char ch = commandText[currentPos];
if (ch == '\'')
{
flag = !flag;
}
else if (ch == ';' && !flag)
{
string tempStr = commandText.Substring(lastPos, currentPos - lastPos).Trim();
if (!string.IsNullOrEmpty(tempStr))
{
tempList.Add(tempStr);
}
lastPos = currentPos + 1;
}
}
if (lastPos < commandText.Length)
{
string lastStr = commandText.Substring(lastPos, commandText.Length - lastPos).Trim();
if (!string.IsNullOrEmpty(lastStr))
{
tempList.Add(lastStr);
}
}
return tempList.ToArray();
}
catch (Exception){ }
}
根据#5楼的代码稍作修改。大家帮忙看一下,还有没有问题了。