.NET OleDb参数化查询无法正常工作 [英] .NET OleDb parameterized query not working as expected

查看:74
本文介绍了.NET OleDb参数化查询无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将此Update命令传递给数据库,它可以正常完成,没有错误,但是它不更新数据库,我不明白为什么?

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中的 order .

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆