MySQL的最后插入身份证,连接器.NET [英] MySql Last Insert ID, Connector .net

查看:190
本文介绍了MySQL的最后插入身份证,连接器.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的MySQL Connector .net和我需要的最后一个查询产生的插入ID。现在,我想 MySqlHelper.ExecuteNonQuery 的返回值应该是最后插入的id,但它只是返回1。

I'm using the MySql Connector .net, and I need to get the insert id generated by the last query. Now, I assume the return value of MySqlHelper.ExecuteNonQuery should be the last insert id, but it just returns 1.

在code我使用的是:

The code I'm using is:

int insertID = MySqlHelper.ExecuteNonQuery(Global.ConnectionString, 
"INSERT INTO test SET var = @var", paramArray);

然而 insertID 始终为1。我试图创建MySQL连接,并打开/关闭手动这就造成了相同的行为

However insertID is always 1. I tried creating a MySql connection and opening/closing manually which resulted in the same behaviour

推荐答案

MySqlHelper.ExecuteNonQuery 返回受影响的行数,而不是最后插入的ID,请的阅读文档

MySqlHelper.ExecuteNonQuery returns the number of affected rows, not the last inserted id, please read the documentation!

至于你的问题:您可以执行以下命令来检索在MySQL中最后插入 AUTO_INCREMENT 值:

As to your problem: You can execute the following command to retrieve the last inserted AUTO_INCREMENT value in MySQL:

SELECT last_insert_id()

请参阅此处查看文档。在MS SQL,来检索最后插入IDENTITY值,你会使用:

See here for the documentation. In MS SQL, to retrieve the last inserted IDENTITY value, you'd use:

SELECT @@IDENTITY
SELECT SCOPE_IDENTITY()
SELECT IDENT_CURRENT('table_name')

请参阅这里的细节;你可能想使用 SCOPE_IDENTITY

See here for details; you'll probably want to use SCOPE_IDENTITY.

这篇关于MySQL的最后插入身份证,连接器.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆