如何替换sql语句中的“'”单引号,防止程序出错。

解决方案 »

  1.   

    dim strSQL as stringstrSQL = strSQL.Replace("'", "''")
      

  2.   

    比如以下语句就不能使用Suron128(辰龙) 的方法
    select * from tbTest where aCol = '123' and bCol <> 'w_w'c-c'
      

  3.   

    把内容当参数传递就不用担心了。如
    select * from table where NewsID=@NewsID
      

  4.   

    在程序中已经大量使用了没有参数的sql语句,不好修改。
    还有更好的办法吗?
      

  5.   

    dim STR_SQl as string,TableName as string
    str_SQL="select * from '" & TableName & "'"
      

  6.   

    Function ReqStr(as_Name)
        ReqStr = Replace ( Trim(Request(as_Name)), "'", "''" )
    End Function
      

  7.   

    '123' = "'" & "123" & "'"
      

  8.   

    扩展问题:
    public static string Encode(String s)
    {
    String tempstr="";
    tempstr+=s.Replace("<","&lt;");
    tempstr=tempstr.Replace(">","&gt;");
    tempstr=tempstr.Replace(" ","&nbsp;");
    tempstr=tempstr.Replace("\'","\"");
    return tempstr;
    }
    public static string Decode(String s)
    {
    String tempstr="";
    tempstr+=s.Replace("&lt;","<");
    tempstr=tempstr.Replace("&gt;",">");
    tempstr=tempstr.Replace("&nbsp;"," ");
    tempstr=tempstr.Replace("\"","\'");
    return tempstr;
    }
      

  9.   

    MySql的参数写法是怎样的?
    我这样写了为什么没效果?
    MyCommand = new MySqlCommand("update inform set inf_title=@title,inf_code=@code,inf_content=@content where inf_id=@id",DBConn);
    MyCommand.Parameters.Add("@title",txttitle.Text);
    MyCommand.Parameters.Add("@code",txtcode.Text);
    MyCommand.Parameters.Add("@content",FreeTextBox1.Text);
    MyCommand.Parameters.Add("@id",Int32.Parse(Request.QueryString["infid"]));
      

  10.   

    呵呵,我比较懒,我在word文档里找了个比较象的字符,然后替换。呵呵。
      

  11.   

    将一个单引号替换成两个单引号,在Sql中就被当作一个单引号字符处理了!
    不信可以试试!
      

  12.   

    代码到了完整的SQL语句的时候再想替换已经来不及了。例如UI上有个输入框采集字符类型数据进行查询:我可以再输入框中输入fdsj';exec sp_addlogin N'ss', N'password'; exec sp_addsrvrolemember N'ss
    这样构造出来的语句是没有任何错误的,但是后果缺.......
      

  13.   

    这个是ASP下的防注入.修改下就是.NET的了.根据在页顶调用下就可以了.
    'QueryString检查
    Sub QSqlSafe()
    Dim BadSql, ArrBad, GetQ, i
    BadSql = "'|and|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare" 
    ArrBad = Split(BadSql,"|") 
    If Request.QueryString <>"" Then 
     For Each GetQ In Request.QueryString 
      For i = 0 To Ubound(ArrBad) 
       If Instr(Request.QueryString(GetQ),ArrBad(i)) > 0 Then 
        Response.Write "forbid" 
        Response.End()
       End if 
      Next 
     Next 
    End If
    End Sub
    'form提交检查
    Sub FSqlSafe()
    Dim BadSql, ArrBad, GetQ, i
    BadSql = "'|and|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare" 
    ArrBad = Split(BadSql,"|") 
    If Request.Form <>"" Then 
     For Each GetQ In Request.QueryString 
      For i = 0 To Ubound(ArrBad) 
       If Instr(Request.QueryString(GetQ),ArrBad(i)) > 0 Then 
        Response.Write "forbid" 
        Response.End()
       End if 
      Next 
     Next 
    End If
    End Sub
      

  14.   

    据说sql语句中4个'代表一个字符'
      

  15.   

    最早的错是在SQL中直接用',而不用参数;
    现在只能自己写一个函数来扫描SQL,动态判断那些'是SQL 内的,那些是外的了,判断逻辑应该不会太难!
      

  16.   

    如何替换sql语句中的“'”单引号,防止程序出错。
    ————————————————————————————————————————
    问题错!替换sql语句中的单引号干什么????只需要替换sql语句中字符串(这些通常被放在单引号中间)中间的单引号。
      

  17.   

    dim strSQL as stringstrSQL ="select * from tbTest where aCol = '"+ aCol..Replace("'", "''")+"'"
      

  18.   

    替换SQL中的单引号,太勉强了。select * from tbTest where aCol = 'select * from tbTest where aCol ......'......这样的,你知道它是怎么出来的?很困难。