.NET OleDb 参数化查询未按预期工作 [英] .NET OleDb parameterized query not working as expected
问题描述
我正在尝试将此更新命令传递给数据库,它可以正常完成,没有错误,但它没有更新数据库,我不明白为什么?
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()
推荐答案
您需要颠倒向 OleDbCommand 对象添加参数的顺序.OleDb 允许我们为参数分配名称,但它忽略名称,只关注参数在 CommandText 中出现的顺序.
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.
因此,由于您的 SQL 语句引用了 Htype
和 Hdate
,因此您需要以相同的顺序添加参数.
Therefore, since your SQL statement refers to Htype
and then Hdate
you need to add the parameters in that same order.
这篇关于.NET OleDb 参数化查询未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!