【问题标题】:vb.net how to increment id if i click the button如果我单击按钮,vb.net 如何增加 id
【发布时间】:2013-03-12 03:43:00
【问题描述】:

我只想增加插入的id,限制等于其他表

我没意见

唯一的问题是,我不能在 id 上增加代码直到达到限制

id 列 2

1------------0

2------------5

3-----------0

以此类推,像这样,通过单击插入按钮,id 应该一一递增直到限制

谁能帮帮我

    RecordDate.Text = MyFormat

    Dim antinull As Integer = Format(0)
    BadBeg.Text = antinull
    WarePcs.Text = antinull
    CustPcs.Text = antinull
    Returned.Text = antinull
    BadEnd.Text = antinull

   Try
        Str = "insert into BadWarehouseInv values("
        Str += Id.Text.Trim()
        Str += ","
        Str += """" & RecordDate.Text.Trim() & """"
        Str += ","
        Str += """" & BadBeg.Text.Trim() & """"
        Str += ","
        Str += WarePcs.Text.Trim()
        Str += ","
        Str += CustPcs.Text.Trim()
        Str += ","
        Str += Returned.Text.Trim()
        Str += ","
        Str += """" & BadEnd.Text.Trim() & """"
        Str += ")"
        Con.Open()
        Cmd = New OleDbCommand(Str, Con)
        Cmd.ExecuteNonQuery()
        Dst.Clear()
        Dad = New OleDbDataAdapter("SELECT * FROM BadWarehouseInv ORDER BY Id", Con)
        Dad.Fill(Dst, "stock")

        Con.Close()
    Catch ex As Exception
        MessageBox.Show("Could Not Insert Record!!!")
        MsgBox(ex.Message & " -  " & ex.Source)
        Con.Close()

【问题讨论】:

标签: vb.net ms-access auto-increment


【解决方案1】:

我不确定我是否理解您的问题,但是:-

通常,当您写入数据库时​​,您有一列是Identity type,并且是表中的唯一标识符或Primary Key。当添加新行时,它会自动增加。

因此,当您在表中创建新行时,您让数据库引擎担心这一点,然后使用a technique to read this value back

如果您不想使用标识列,您可以使用查询来查找当前最高的数字,然后将其加一。不建议这样做,因为您可能有两个客户端同时执行此操作,因此其中一个客户端在尝试写入数据库时​​会失败。

【讨论】:

    猜你喜欢
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多