这个查询有什么问题?我提供了所有参数的值

时间:2016-08-24 16:25:13

标签: sql vb.net ms-access parameters

我有一个带有更新功能的vb.net项目,我从保存按钮点击方法将值传递到数据库SQL函数。

在我正在更新的数据库表中,有10个字段。正如您在下面的查询中所看到的,我提供了10个值,每个字段一个。

然而,当它遇到Dc.ExecuteNonQuery()行时,它会输出错误

  

没有给出一个或多个必需参数的值

我无法解决原因 - 其他人可以吗?

Public Shared Function UpdateFreight(ByVal OrderNumber As Integer, _
                                     ByVal OrderRef As String, _
                                     ByVal FreightNum As Integer, _
                                     ByVal FreightVal As Double, _
                                     ByVal FreightDesc As String, _
                                     ByVal ProductCode As String, _
                                     ByVal EstDelivery As DateTime, _
                                     ByVal SCode As String, _
                                     ByVal CCode As String, _
                                     ByVal AddressCode As String, _  
                                     con As OleDbConnection)

    Try
        Dim Dc As New OleDbCommand
        Dc.Connection = con

        Dc = New OleDb.OleDbCommand("UPDATE [Order_Freight] SET [Order_Number] = ?, _
                                            [Order_Reference] = ?, _
                                            [Freight_Number] = ?, [Freight_Val] = ?, _
                                            [Freight_Desc] = ?, [Product_Code] = ?,_
                                            [Est_Delivery] = ?, [Supplier_Code] = ?, _
                                            [Customer_Code] = ?, [Address_Code] = ? _
                                            WHERE [OrderNumber] = ?", con)

        Dc.Parameters.AddWithValue("@ono", OrderNumber)
        Dc.Parameters.AddWithValue("@oref", OrderRef)
        Dc.Parameters.AddWithValue("@fnum", FreightNum)
        Dc.Parameters.AddWithValue("@fval", FreightVal)
        Dc.Parameters.AddWithValue("@fdesc", FreightDesc)
        Dc.Parameters.AddWithValue("@pcode", ProductCode)
        Dc.Parameters.AddWithValue("@estd", EstDelivery)
        Dc.Parameters.AddWithValue("@scode", SCode)
        Dc.Parameters.AddWithValue("@ccode", CCode)
        Dc.Parameters.AddWithValue("@acode", AddressCode)
        Dc.Parameters.AddWithValue("@onum", OrderNumber)

        Dc.ExecuteNonQuery()

    Catch ex As Exception
        errorLog(ex)
        MsgBox("Failed to update data, refer to error log")
        Throw

    End Try
End Function

如果有帮助,这就是调用函数的地方

    database.UpdateFreight(OrderNumber, OrderRef, FNum, FreightVal, FreightDesc, PCode, EstDate, SCode, custCode, Address, con)

并澄清所有参数都被赋予值

enter image description here

(另外,它不是代码格式化,我只是为了问题的可读性而调整了格式,它在vb中看起来不像这样)

2 个答案:

答案 0 :(得分:2)

我认为问题是因为您的列名不清楚

UPDATE [Order_Freight] SET [Order_Number] = ?, 
[Order_Reference] = ?, 
[Freight_Number] = ?, [Freight_Val] = ?, 
[Freight_Desc] = ?, [Product_Code] = ?,
[Est_Delivery] = ?, [Supplier_Code] = ?, 
[Customer_Code] = ?, [Address_Code] = ? 
WHERE [OrderNumber] = ?

注意SET [Order_Number] = ?WHERE [OrderNumber] = ?。其中一个可能不正确,并被解释为未知列,因此Access将其视为参数。修正订单号列上的拼写是正确的。

答案 1 :(得分:1)

由于OleDb不支持命名参数,并查看OleDbCommand.Parameters的{​​{3}}文档,如果您将参数作为OleDbParameter传递,可能会有效对象呢?例如 -

 Dc = New OleDb.OleDbCommand("UPDATE [Order_Freight] SET [Order_Number] = ?, _
                                        [Order_Reference] = ?, _
                                        [Freight_Number] = ?, [Freight_Val] = ?, _
                                        [Freight_Desc] = ?, [Product_Code] = ?,_
                                        [Est_Delivery] = ?, [Supplier_Code] = ?, _
                                        [Customer_Code] = ?, [Address_Code] = ? _
                                        WHERE [OrderNumber] = ?", con)

        Dc.Parameters.Add(New OleDbParameter("@ono", OrderNumber))
        Dc.Parameters.Add(New OleDbParameter("@oref", OrderRef))
        Dc.Parameters.Add(New OleDbParameter("@fnum", FreightNum))
        Dc.Parameters.Add(New OleDbParameter("@fval", FreightVal))
        Dc.Parameters.Add(New OleDbParameter("@fdesc", FreightDesc))
        Dc.Parameters.Add(New OleDbParameter("@pcode", ProductCode))
        Dc.Parameters.Add(New OleDbParameter("@estd", EstDelivery))
        Dc.Parameters.Add(New OleDbParameter("@scode", SCode))
        Dc.Parameters.Add(New OleDbParameter("@ccode", CCode))
        Dc.Parameters.Add(New OleDbParameter("@acode", AddressCode))
        Dc.Parameters.Add(New OleDbParameter("@onum", OrderNumber))

        Dc.ExecuteNonQuery()