上面忘记说了,RemoveSqlComment是删除SQL注释的方法(返回string)

解决方案 »

  1.   


     string[] sqlStrs = longSQLstr.Split(';');
    随便说下,楼主写的那个SQL不正确,里面是逗号,不是分号;
      

  2.   

    我不是分隔多个字段的啊,为啥用逗号?我是一个字符串';;;;;;'   作为name字段的值插入的啊。
      

  3.   

    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)
    }
      

  4.   

    只接用一个特殊字符作为sql语句的分割(这个特殊字符是在sql中不可能使用到的),然后用split就可以了呀。
      

  5.   

    我要运行标准的SQL脚本的啊。标准的SQL脚本是以分号分割的。
      

  6.   


    if(signcount%2!=0)最后这个没看懂。signcount这个变量干吗用的?分号的个数?为什么分号的个数为奇数的时候才添加剩余的字符串呢?
      

  7.   

            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楼的代码稍作修改。大家帮忙看一下,还有没有问题了。