我正在尝试通过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
答案 0 :(得分: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()