我不确定您的execSQL()
方法是如何构建的,但将可变数据直接替换为SQL查询字符串并不正常,或者完全可以。这是让你的程序被黑客攻击的好方法。相反,您需要一种机制来分别接受数据并将其发送到服务器。这通常看起来是这样的:
Public Sub execSQL(ByVal SQL As String, Optional ByVal ParamArray QueryParameters() As MySqlParameter)
Using cn As New MySqlConnection("connection string here"), _
cmd As New MySqlCommand(SQL, cn)
If QueryParameters IsNot Nothing Then
For Each p As MySqlParameter In QueryParameters
'This does NOT EVER do string manipulation to set the data into the query
' Instead, it's sent to the server in a separate data section, where the variables are setup.
' In this way, data stays separate from code, and any possibility of sql injection is prevented.
cmd.Parameters.Add(p)
Next
End If
cn.Open()
cmd.ExecuteNonQuery()
End Using
End Sub
然后调用它像这样:
Public Sub Code(ByVal dte As DateTime)
Dim strSQl As String = "Update tblTenant Set dte = @dte blablalbla"
Dim dteParam As New MySqlParameter("@dte", MySqlDbType.DateTime)
dteParam.Value = dte
Me._Inst.execSQL(strSQl, dteParam)
End Sub
我见过很多情况下,切换到查询参数也是固定的一个棘手的格式或语法问题,所以我相信在这里使用查询参数很可能会解决引发问题的问题。
什么是列类型? –
这是疯狂的容易受到SQL注入攻击。 –
使用SQL参数将'DateTime'作为'DateTime'传递。该代码传递文本并让DB提供者解决问题。 – Plutonix