Return Last ID (IDENTITY) Insert VB.NET MySQL string

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()) 

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

+7
source share
3 answers

You can use a double query and use the LAST_INSERT_ID () function. After running the first query, 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) 
+13
source

You can get the AUTO_INCREMENT value for the table through a MySQL query and then show what's in your MsgBox

+1
source

A bit late for the party, but after

 cmd_query.ExecuteScalar() MsgBox(cmd_query.LastInsertedId) 

Creates the last insert identifier.

0
source

Source: https://habr.com/ru/post/911221/


All Articles