参数化SQL查询

时间:2019-08-20 09:45:29

标签: mysql sql vb.net parameterized

我想参数化一些SQL语句,以便我的代码不再受SQL注入的侵害,但是我实际上没有计划如何参数化例如where子句。

Dim accID As String = DatabaseConnecter.readField("SELECT ID FROM accounts WHERE accountname ='" & user & "' AND password='" & pw & "';")

问题是如果您键入给定的用户名,例如测试并扩展用户名。您无需输入密码即可登录。

编辑:

Public Function readField(ByVal sql As String) As String
        Dim output As String = "ERROR"
        Using cn = New MySqlConnection(connString.ToString())
            Using cmd = New MySqlCommand(sql, cn)
                cn.Open()
                Using rd = cmd.ExecuteReader()
                    Try
                        rd.Read()
                        output = rd.GetString(0)
                        rd.Close()
                    Catch ex As Exception
                    End Try
                End Using
                cn.Close()
            End Using
        End Using
        Return output
    End Function
´´´

2 个答案:

答案 0 :(得分:0)

Private Function GetID(User As String, pw As String) As String
    Using cmd As New SqlCommand("SELECT ID FROM accounts WHERE accountname =@user AND password=@password", New SqlConnection(SQLConnString))
        cmd.Parameters.AddWithValue("@user", User)
        cmd.Parameters.Add("@password", SqlDbType.NVarChar)
        cmd.Parameters("@password").Value = pw
        Try
            cmd.Connection.Open()
            Return cmd.ExecuteScalar()
        Catch ex As Exception
            'handle error
            Return Nothing
        Finally
            cmd.Connection.Close()
        End Try
    End Using

End Function

我已经演示了两种设置参数的方法。搜索更多信息或进行比较。

答案 1 :(得分:0)

要进行参数化查询,您需要创建参数并编写适当的SQL文本,其中包含参数占位符,以代替直接从用户输入的值。

例如,您的sql文本应该是这样的

Dim sqlText = "SELECT ID FROM accounts WHERE accountname =@name AND password=@pwd"

现在您已经有了一个参数化的文本,但是仍然需要创建将与您的sql命令一起发送到数据库引擎的参数。

您可以在调用执行查询的方法之前以这种方式创建参数(在这种情况下为两个)

Dim p1 as MySqlParameter = new MySqlParameter("@name", MySqlDbType.VarChar)
p1.Value = user  

Dim p2 as MySqlParameter = new MySqlParameter("@pwd", MySqlDbType.VarChar)
p2.Value = password

Dim pms As List(Of MySqlParameter) = new List(Of MySqlParameter)()
pms.Add(p1)
pms.Add(p2)

现在,我们需要将此列表传递给您的方法(这需要更改您的方法签名)

DatabaseConnecter.readField(sqlText, pms)

方法本身应更改为

Public Function readField(ByVal sql As String, Optional pms As List(Of MySqlParameter) = Nothing) As String
    Dim output As String = "ERROR"
    Using cn = New MySqlConnection(connString.ToString())
        Using cmd = New MySqlCommand(sql, cn)
            cn.Open()

            ' This block adds the parameter defined by the caller to the command
            ' The parameters are optional so we need to check if we have really received the list or not
            if pms IsNot Nothing Then
                cmd.Parameters.AddRange(pms.ToArray())
            End If
            Using rd = cmd.ExecuteReader()
                Try
                    rd.Read()
                    output = rd.GetString(0)
                    rd.Close()
                Catch ex As Exception
                End Try
            End Using
            ' no need to close when inside a using block
            ' cn.Close()
        End Using
    End Using
    Return output
End Function

该方法现在具有可选参数,该参数将包含查询所需的参数列表(如果您的查询不需要参数,则不包含任何参数)。该列表被添加到命令参数集合中,并且现在执行查询。

最终说明:将明文密码存储到数据库是一个众所周知的安全问题。建议您搜索how to store passwords in a database.