使用c#从mysql查询返回最后一个插入ID

使用c#从mysql查询返回最后一个插入ID

本文介绍了使用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.

推荐答案

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



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

08-06 18:18