在插入行时返回最后一个 ID (IDENTITY) VB.NET MySQL

2021-12-06 00:00:00 identity vb.net mysql
Dim insert_coupon_query As String = ("INSERT INTO qa_discountcoupons (id, status_code) VALUES (AUTO_INCREMENT_ID, 5)")
                Dim cmd_query As New MySqlCommand(insert_coupon_query, objConn)
                Dim cmd_result As Integer = CInt(cmd_query.ExecuteScalar())

我想返回当前插入的 AUTO_INCREMENT 值,并显示在 msgbox 中.

I want to return the AUTO_INCREMENT value of the current insert, and show in a msgbox.

推荐答案

您可以使用双查询并使用函数 LAST_INSERT_ID() 运行您的第一个查询以获取最后一个当前查询后:

You can use an double Query and use the function LAST_INSERT_ID() After run your first query to get the last current query:

Dim insert_coupon_query As String = ("INSERT INTO qa_discountcoupons (status_code) VALUES (5); SELECT LAST_INSERT_ID()")
                Dim cmd_query As New MySqlCommand(insert_coupon_query, objConn)
                Dim cmd_result As Integer = CInt(cmd_query.ExecuteScalar())

                MsgBox(cmd_result)

相关文章