Return Last ID (IDENTITY) On Insert row VB.NET MySQL
31,011
Solution 1
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)
Solution 2
Bit late to the party but after
cmd_query.ExecuteScalar()
MsgBox(cmd_query.LastInsertedId)
Produces the last insert id.
Solution 3
You can fetch the AUTO_INCREMENT value for a table through a MySQL query and then show that in your MsgBox
Author by
John Nuñez
Updated on July 09, 2022Comments
-
John Nuñez almost 2 years
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 a msgbox.
-
surpavan over 7 yearsNever thought we could use a double query in vb.net sql statement. It helps to get the auto-increment number from ID column. Thank you.