代码之家  ›  专栏  ›  技术社区  ›  Harambe

当变量有撇号时,字符串生成器选择查询

  •  0
  • Harambe  · 技术社区  · 8 年前

    我的一位同事创建了一个程序,它读取一个文本文件,并将其中的各种值赋给sql语句中使用的变量。 其中一个变量, gsAccounts 是字符串变量。

    使用字符串生成器, SELECT 声明是建立在 sql.append . 在字符串的末尾,有以下行:

    sql.Append(" WHERE L.Account_Code IN(" & gsAccounts & ")"
    

    我现在的问题是有时候,不总是, 政府帐户 (帐户代码列表)可以包含带有撇号的帐户代码,因此查询变为

    "WHERE L.Account_Code IN('test'123')"
    

    当帐户代码为 test'123

    我试着用双引号在 "WHERE L.Account_Code IN("""" & gsAccounts & """")" 方式(使用4和6“相邻,但都不起作用)

    我怎么能避开这个?帐户代码是表中的主键,因此我不能删除它,因为有很多年的交易和数据都与之相关。

    3 回复  |  直到 8 年前
        1
  •  1
  •   jmcilhinney    8 年前

    我发布了以下示例 here 十年前,几乎是今天。(哎呀!以为是6月5日,但现在是1月5日。10.5年。)

    Dim connection As New SqlConnection("connection string here")
    Dim command As New SqlCommand
    Dim query As New StringBuilder("SELECT * FROM MyTable")
    
    Select Case Me.ListBox1.SelectedItems.Count
        Case 1
            'Only one item is selected so we only need one parameter.
            query.Append(" WHERE MyColumn = @MyColumn")
            command.Parameters.AddWithValue("@MyColumn", Me.ListBox1.SelectedItem)
        Case Is > 1
            'Multiple items are selected so include a parameter for each.
            query.Append(" WHERE MyColumn IN (")
    
            Dim paramName As String
    
            For index As Integer = 0 To Me.ListBox1.SelectedItems.Count - 1 Step 1
                'Name all parameters for the column with a numeric suffix.
                paramName = "@MyColumn" & index
    
                'Add a comma before all but the first value.
                If index > 0 Then
                    query.Append(", ")
                End If
    
                'Append the placeholder to the SQL and add the parameter to the command
                query.Append(paramName)
                command.Parameters.AddWithValue(paramName, Me.ListBox1.SelectedItems(index))
            Next index
    
            query.Append(")")
    End Select
    
    command.CommandText = query.ToString()
    command.Connection = connection
    
        2
  •  0
  •   David    8 年前

    单引号可以“转义”为双引号。例如。 ' 变成 '' .

    但是,由于sql注入的高风险(一个非常危险且普遍存在的问题),通常不建议使用这种方法。见: https://www.owasp.org/index.php/SQL_Injection

    为了避免这种情况,大多数库将包括某种类型的逃避机制,包括在Java世界中使用准备好的语句之类的东西。在.NET世界中,这可能有用: https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.prepare(v=vs.110).aspx

        3
  •  0
  •   Alireza Nemeti    8 年前

    如果只有一个字段,这是最简单的解决方案

        Private Function gsAccountsConvert(ByVal gsAccounts As String)
        Dim gsAccountsString As String = ""
        Dim StringTemp
        StringTemp = gsAccounts.Split(",")
        Dim i As Integer
        For i = 0 To UBound(StringTemp)
            StringTemp(i) = StringTemp(i).ToString.Trim
            If StringTemp(i) <> "" Then
                If StringTemp(i).ToString.Substring(0, 1) = "'" Then
                    StringTemp(i) = """" & StringTemp(i).ToString.Substring(1, Len(StringTemp(i).ToString) - 2) & """"
                End If
            End If
            If i <> UBound(StringTemp) Then
                gsAccountsString = gsAccountsString & StringTemp(i).ToString.Replace("'", "''") & ","
            Else
                gsAccountsString = gsAccountsString & StringTemp(i).ToString.Replace("'", "''") & ""
            End If
        Next
        gsAccountsString = gsAccountsString.Replace("""", "'")
        Return gsAccountsString
    End Function