我不確定您的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