在插入行时返回最后一个 ID (IDENTITY) VB.NET MySQL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9791156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Return Last ID (IDENTITY) On Insert row VB.NET MySQL
提问by John Nu?ez
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.
我想返回当前插入的 AUTO_INCREMENT 值,并显示在 msgbox 中。
回答by blackriderws
You can use an double Query and use the function LAST_INSERT_ID()After run your first query to get the last current query:
您可以使用双查询并使用函数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)
回答by sikander
You can fetch the AUTO_INCREMENT value for a table through a MySQL queryand then show that in your MsgBox
您可以通过MySQL 查询获取表的 AUTO_INCREMENT 值,然后在您的 MsgBox 中显示
回答by Ian Halligan
Bit late to the party but after
参加派对有点晚,但之后
cmd_query.ExecuteScalar()
MsgBox(cmd_query.LastInsertedId)
Produces the last insert id.
生成最后一个插入 ID。