我最近开始回顾 vb 中的数据库编程,并且正在使用 Northwind 数据库进行测试。
在我的程序中,我为产品添加、编辑和删除功能。当我使用 Windows 窗体向表中添加新产品时,产品 ID 会递增(如果最后一个产品 ID 为 70,则新产品将为 71),然后我更新数据库。
然而,我一直遇到的一个问题是,当我再次调试我的表单并转到我刚刚输入的最后一个产品时,ID 已从 71 更改为 84,我不确定它为什么会这样做。这是我用于添加功能的代码,如果您需要查看我的代码的任何其他部分,请告诉我并感谢您的帮助:)
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
newProduct = NorthwindDataSet1.Products.NewProductsRow()
newProduct("ProductName") = txtProdName.Text
newProduct("UnitPrice") = txtUnitPrice.Text
newProduct("Discontinued") = cbxDiscontinued.CheckState
newProduct("ProductID") = NewProdID
Try
NorthwindDataSet1.Products.Rows.Add(newProduct)
Try
Me.Validate()
Me.ProductsTableAdapter.Update(Me.NorthwindDataSet1.Products)
MsgBox("Update successful")
Catch ex As Exception
MsgBox("Update failed")
End Try
Catch x As Exception
MsgBox("This does not work")
End Try
btnAdd.Visible = False
btnDelete.Enabled = True
btnCancel.Enabled = True
btnFirst.Enabled = True
btnLast.Enabled = True
btnPrevious.Enabled = True
btnNext.Enabled = True
btnNew.Enabled = True
lblProdID.Visible = True
ProductsBindingSource.ResumeBinding()
lblLastPos.Text = ProductsBindingSource.Count
End Sub