mssql查询帮助查找添加到数据库表中的最后一个数据的主键 [英] mssql query help in finding the primary key of the last data added on a table in a db
本文介绍了mssql查询帮助查找添加到数据库表中的最后一个数据的主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有什么方法可以获取刚刚添加到数据库表中的数据的主键值?假设我们使用插入查询:
字符串qry =插入tbl_student(name,marksObtained,class,section)values(" Arpan,50," 10," A)";
这里的主键是stud_id,其值是自动给出的,
如何检索最近更新到数据库的stud_id的值?
谢谢你的帮助
Arpan
解决方案
在插入/更新查询后尝试选择@@ IDENTITY,
@@ IDENTITY返回最后插入/更新的第一列的值
您可以使用Last_Insert_ID()函数获取最后的自动增量ID.我要做的是像这样在我的命令中添加额外的查询
MySqlCommand cmd = 新 MySqlCommand(" ,connection); int lastID =( int )cmd.ExecuteScalar();
I there any way to get the primary key value of the data we just added to the database table?? suppose we use the insert query:
string qry="insert into tbl_student(name,marksObtained,class,section) values("Arpan",50,"10","A")";
here the primary key is stud_id whose value is given automatically,
how can i retirve the value of stud_id which is recently updated to the database??
thank u for your help
Arpan解决方案Try SELECT @@IDENTITY After the Insert/Update Query,
@@IDENTITY returns the value of first columns Last Inserted/Updated
You can use Last_Insert_ID() function to get the last auto increment id. What I do is add the extra query to my command like this
MySqlCommand cmd = new MySqlCommand("INSERT INTO tbl_student(name,marksObtained,class,section) VALUES ('Arpan',50,'10','A'); LAST_INSERT_ID();", connection); int lastID = (int)cmd.ExecuteScalar();
这篇关于mssql查询帮助查找添加到数据库表中的最后一个数据的主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文