只想问一下ms访问数据库中更新数据的正确方法是什么,因为当我使用这段代码时,我的数据没有更新,也没有显示任何错误,所以函数的返回值是假。 下面是我的代码。
dim conn as new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\myDb.accdb;Persist Security Info=False;")
Protected Function UpdateProduct(ByVal productDetails As ProductModel) As Boolean
reopenConnection()
cmd = New OleDbCommand("UPDATE Product Set ProductName=@prodName, Price=@price, ProductDescription=@prodDesc, CategoryId=@catId where ProductId=@prodId;", conn)
cmd.CommandType = CommandType.Text
cmd.Parameters.Add("@prodId", OleDbType.VarChar).Value = productDetails.NewProductId
cmd.Parameters.Add("@prodName", OleDbType.VarChar).Value = productDetails.ProductName
cmd.Parameters.Add("@price", OleDbType.Decimal).Value = productDetails.Price
cmd.Parameters.Add("@prodDesc", OleDbType.VarChar).Value = productDetails.ProductDescription
cmd.Parameters.Add("@categoryId", OleDbType.Integer).Value = productDetails.CategoryId
Return cmd.ExecuteNonQuery() > 0
End Function
Private Sub reopenConnection()
If conn.State = ConnectionState.Open Then
conn.Close()
End If
conn.Open()
End Sub
由于
答案 0 :(得分:0)
我的访问体验是您必须提交事务,或者您从未在数据库中看到更新。请参阅此stackoverflow线程以获取语法。