使用c#从mysql查询返回最后一个插入ID [英] returning Last Insert ID from mysql query with c#

查看:577
本文介绍了使用c#从mysql查询返回最后一个插入ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有表employee(empID,fName,lName)

position(positionID,positionName)

empPosition(empID,positionID,dateChanged,INDEX(empID),INDEX (positionID))



我正在执行此代码

I have table employee(empID, fName, lName)
position(positionID, positionName)
empPosition(empID, positionID, dateChanged, INDEX(empID), INDEX(positionID))

I am doing this code

cmd.CommandText =
                "INSERT INTO employee (empID, fName, lName)" +
                " VALUES('null','" + fNameTxt.Text + "','" + lNameTxt.Text + "')";"



我必须输入这个empID,这是数据库中的自动增量,与positionID一起输入empPosition表。你能告诉我如何获取LAST_INSERT_ID()吗?

我试过做SET @employee = LA ST_INSERT_ID();但无法使语法正确。


I have to enter this empID, which is auto increment in database, to enter in empPosition table along with positionID. can you give me any idea how to get LAST_INSERT_ID() in it ?
I tried to do SET @employee = LAST_INSERT_ID(); but couldnt make the syntax correct.

推荐答案

您可以从以下查询中获取任何表格的详细信息:

You can get any table's details from the following query:
SHOW TABLE STATUS FROM 'your DB name' LIKE '%your table name%' ;



要获取表格的自动增量值,可以使用以下查询:


For getting the auto increment value on a table you can use the following query:

SELECT AUTO_INCREMENT
FROM  INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'your db name'
AND   TABLE_NAME   = 'your table name';





祝你好运,

OI



Good luck,
OI


使用select SCOPE_IDENTITY()从数据库中获取最后一次成功插入的id值。



这适用于SQL SERVER ..





好​​运。
Use "select SCOPE_IDENTITY()" to get the last successful inserted id value from database.

This works in SQL SERVER..


Best of luck.


这篇关于使用c#从mysql查询返回最后一个插入ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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