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

时间:2012-03-20 16:43:32

标签: mysql vb.net identity

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

3 个答案:

答案 0 :(得分:13)

您可以使用双重查询并使用函数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)

答案 1 :(得分:1)

您可以通过MySQL query获取表格的AUTO_INCREMENT值,然后在MsgBox中显示

答案 2 :(得分:0)

晚到聚会但在

之后
cmd_query.ExecuteScalar()
MsgBox(cmd_query.LastInsertedId)

产生最后一个插入ID。