1

我正在尝试通过 VB.NET 更新 Access 中的记录。到目前为止,这是我的代码。目标是搜索条形码,它返回与之相关的数据,用户可以更改数据并保存更新的数据。除了更新部分,我什么都有。这是我的带有更新代码的保存按钮。我希望我说清楚了。

编辑:对不起。我忘了告诉你我所有的问题。它返回 0 行受影响,没有错误消息。

 Private Sub btnEditProductSave_Click(sender As System.Object, e As System.EventArgs) Handles btnEditProductSave.Click
    'Creates Transaction and Connection Objects
    Dim Transaction As OleDb.OleDbTransaction = Nothing
    Dim connection As OleDb.OleDbConnection = Nothing

    Try

        'Assign the Connection 
        connection = New OleDb.OleDbConnection(My.Settings.POS_NEWConnectionString)
        'Opens the Connection and begins the Transaction
        connection.Open()
        Transaction = connection.BeginTransaction

        Dim barcode As String = txtEditProductBarcode.Text
        Dim name As String = txtEditProductName.Text
        Dim supplier As String = cbEditProductSupplier.SelectedValue
        Dim sell As Decimal = Val(txtEditProductSellPrice.Text)
        Dim quantity As Integer = numQuantity.Value
        Dim discount As Decimal = Val(txtEditProductDiscount.Text)
        Dim department As Integer = Val(txtEditProductDepartment.Text)

        Dim SQL As String = "UPDATE PRODUCT SET ProductName = @productName, SupplierId = @supplier, SellPrice = @sellPrice, Quantity = @quantity, DiscountPrice = @discount, DepartmentNumber = @dept WHERE ProductBarcode = @barcode"

        'Creates the Command for the Transaction
        Dim CT1 As New OleDb.OleDbCommand

        'Assigns a connection and transaction to a new command object
        CT1.Connection = connection
        CT1.Transaction = Transaction


        CT1.Parameters.AddWithValue("@barcode", barcode)
        CT1.Parameters.AddWithValue("@productName", name)
        CT1.Parameters.AddWithValue("@supplier", supplier)
        CT1.Parameters.AddWithValue("@sellPrice", sell)
        CT1.Parameters.AddWithValue("@quantity", quantity)
        CT1.Parameters.AddWithValue("@discount", discount)
        CT1.Parameters.AddWithValue("@dept", department)

        CT1.CommandText = SQL

        Dim number As Integer = CT1.ExecuteNonQuery

        MsgBox(number & " of row were affected")

        CT1.Dispose()

    Catch ex As System.Exception
        System.Windows.Forms.MessageBox.Show(ex.Message)
    End Try
End Sub
4

1 回答 1

1

OleDb 中的参数按索引顺序排列,而不是名称。

试试这种方式(条形码参数最后):

    CT1.Parameters.AddWithValue("@productName", name)
    CT1.Parameters.AddWithValue("@supplier", supplier)
    CT1.Parameters.AddWithValue("@sellPrice", sell)
    CT1.Parameters.AddWithValue("@quantity", quantity)
    CT1.Parameters.AddWithValue("@discount", discount)
    CT1.Parameters.AddWithValue("@dept", department)
    CT1.Parameters.AddWithValue("@barcode", barcode)

你也有一个BeginTransaction但没有提交。

它应该看起来像这样:

Dim tx As OleDb.OleDbTransaction = connection.BeginTransaction

// blah - blah - blah

tx.Commit()
于 2012-10-30T19:19:39.487 回答