添加表单时,产品的ID号会不断更改

时间:2022-10-20 03:36:11

I have recently began to go back over database programming in vb and I am using the Northwind database to test with.

我最近开始回到vb中的数据库编程,我正在使用Northwind数据库进行测试。

On my program I have add, edit and delete functionality for the Products. When I add new product to the table using Windows Forms, the Product ID is incremented fine (If the last product ID is 70 the new product will be 71) and then I update the database.

在我的程序中,我为产品添加,编辑和删除功能。当我使用Windows窗体向表中添加新产品时,产品ID会正常增加(如果最后一个产品ID为70,则新产品将为71),然后我更新数据库。

However a problem I keep having is when say I debug my form again and go to the last product that I just entered, the ID has changed from 71 to 84 and I am unsure as to why it has done this. Here is the code that I have for the add function, if you need to see any other parts of my code then let me know and thanks for the help :)

然而,我一直遇到的问题是,当我再次调试我的表单并转到我刚刚输入的最后一个产品时,ID已从71更改为84,我不确定它为什么这样做。这是我对add函数的代码,如果你需要查看我的代码的任何其他部分,那么让我知道并感谢帮助:)

 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

1 个解决方案

#1


0  

Regardless if your insert query goes completed or not, every insert attempt to an Access database table will increase the auto number field.

无论您的插入查询是否完成,每次对Access数据库表的插入尝试都将增加自动编号字段。

it is common, auto numbered fields will produce gaps from like deleted records or failed inserts. You can only maintain continuous numbering if your table never perform delete action or if you have a custom id field and reset the counter upon deletion of records. This will also require manually increment your custom id.

这很常见,自动编号的字段会产生类似已删除记录或失败插入的间隙。如果您的表从不执行删除操作或者您有自定义ID字段并且在删除记录时重置计数器,则只能保持连续编号。这还需要手动增加您的自定义ID。

Best way just ignore the gaps.. :)

最好的方法只是忽略差距.. :)

#1


0  

Regardless if your insert query goes completed or not, every insert attempt to an Access database table will increase the auto number field.

无论您的插入查询是否完成,每次对Access数据库表的插入尝试都将增加自动编号字段。

it is common, auto numbered fields will produce gaps from like deleted records or failed inserts. You can only maintain continuous numbering if your table never perform delete action or if you have a custom id field and reset the counter upon deletion of records. This will also require manually increment your custom id.

这很常见,自动编号的字段会产生类似已删除记录或失败插入的间隙。如果您的表从不执行删除操作或者您有自定义ID字段并且在删除记录时重置计数器,则只能保持连续编号。这还需要手动增加您的自定义ID。

Best way just ignore the gaps.. :)

最好的方法只是忽略差距.. :)