SQL注入 - 我应该只使用String替换?

Min*_*Min -1 sql vb.net replace sql-injection

我不太了解SQL注入,但我想要一个简单的答案,因为我只想知道我是否应该只在一个字符串或所有字符串中放置替换(a,"'","''")案件?

vb.net中的示例

"select email from users where id=" & Replace(myId, "'", "''")
Run Code Online (Sandbox Code Playgroud)

和

"select email from users where surname=" & Replace(mySurname, "'", "''")
Run Code Online (Sandbox Code Playgroud)

第一种情况下的替换是否无用?

编辑:

我的问题不是"什么是最好使用而不是替换",但如果我必须使用替换也使用非字符串值.谢谢!

Tha*_*kou 6

最好使用参数化查询.

例如:

Public Function GetBarFooByid(ByVal id As String) As String
    Dim sql As String = "select email from users where id= @id"

    Using cn As New SqlConnection("Your connection string here"), _
        cmd As New SqlCommand(sql, cn)

        cmd.Parameters.Add("@id", SqlDbType.VarChar, 50).Value = "id"
        Return cmd.ExecuteScalar().ToString()
    End Using

End Function
Run Code Online (Sandbox Code Playgroud)