如何插入一条记录,并使用一个单一的SqlCommand返回新创建的ID? [英] How to insert a record and return the newly created ID using a single SqlCommand?
本文介绍了如何插入一条记录,并使用一个单一的SqlCommand返回新创建的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用SqlCommand对象中插入一条记录入表自动生成主键。我怎么能写命令文本,使我得到新创建的ID,当我使用ExecuteScalar()方法?
解决方案
INSERT INTO YourTable(VAL1,val2中,VAL3 ...)
VALUES(@ VAL1,@ val2中,@ VAL3 ...);
SELECT SCOPE_IDENTITY();
在每个语句的结束,不要忘了分号。
I'm using an SqlCommand object to insert a record into a table with an autogenerated primary key. How can I write the command text so that I get the newly created ID when I use the ExecuteScalar() method?
解决方案
INSERT INTO YourTable(val1, val2, val3 ...)
VALUES(@val1, @val2, @val3...);
SELECT SCOPE_IDENTITY();
Don't forget the semicolons at the end of each statement.
这篇关于如何插入一条记录,并使用一个单一的SqlCommand返回新创建的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文