小编典典

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

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中显示。


阅读 316

收藏
2020-05-17

共1个答案

小编典典

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

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)
2020-05-17