.NET OleDb parameterized query not working as expected

我只是一个虾纸丫 提交于 2019-11-27 08:38:05

问题


I'm trying to pass this Update command to a database, it completes ok with no errors but it doesnt update the database and I can't understand why?

    Dim Cmd As OleDbCommand
    Dim SQL As String
    Dim objCmd As New OleDbCommand

    Dim Con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & My.Settings.MasterPath)
    Dim Item = "08/08/2015"


            SQL = ("UPDATE [Hol Dates] SET [" & EmployeeList.Text & "]= @Htype WHERE [HDate]=@Hdate")

    Cmd = New OleDbCommand(SQL, Con)
    objCmd = New OleDbCommand(SQL, Con)
    objCmd.Parameters.AddWithValue("@Hdate", item)
    objCmd.Parameters.AddWithValue("@Htype", "None")
    Con.Open()
    Dim ans = MsgBox("Do you want to unbook this holiday?", MsgBoxStyle.YesNo)

    If ans = MsgBoxResult.Yes Then
        objCmd.ExecuteNonQuery()
    Else
        Exit Sub
    End If

    Con.Close()
    Con.Dispose()

回答1:


You need to reverse the order in which you add the parameters to the OleDbCommand object. OleDb allows us to assign names to parameters but it ignores the names and only pays attention to the order in which the parameters appear in the CommandText.

Therefore, since your SQL statement refers to Htype and then Hdate you need to add the parameters in that same order.



来源:https://stackoverflow.com/questions/32004709/net-oledb-parameterized-query-not-working-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!