SQL INSERT命令后,在/返回标识列的值 [英] Returning the value of an identity column during/after SQL INSERT command
问题描述
使用VS 2010,用ASP.NET和.NET 3.0和C#...
Using VS 2010, with ASP.NET, and .NET 3.0, and C#...
当我使用System.Web.UI.WebControls.SqlDataSource并调用其插入()方法来插入新行,并且该表有标识列,我想我的方法返回列的值
When I use a System.Web.UI.WebControls.SqlDataSource and call its Insert() method to insert a new row, and that table has an identity column, I'd like my method to return the value of that column.
例如,在我的SQL 2005的表,我有:
For example, in my SQL 2005 table, I've got:
- Customer.Id
- Customer.FirstName
- Customer.LastName
在哪里Customer.Id是一种身份科拉姆。
Where Customer.Id is an identity colum.
当我打电话给我的方法InsertNewCustomerRecord(约翰福音,史密斯夫妇),我希望能够返回,获取数据库自动生成Customer.Id。
When I call my method InsertNewCustomerRecord( "John", "Smith" ), I'd like to be able to return the Customer.Id that gets automatically generated in the database.
对不起,这样的粗略提出的问题。让我知道如果我可以添加更好的细节。谢谢你。
Sorry for such a roughly posed question. Let me know if I can add better detail. Thanks.
推荐答案
的的插入方法使用的<一个href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.insertcommand.aspx"相对=nofollow>的InsertCommand 财产...
The Insert method uses the InsertCommand property...
那么修改将InsertCommand是要么
So modify the InsertCommand to be either
INSERT Mytable (col1, col2) VALUES (@param1, @parame);SELECT SCOPE_IDENTITY();
或(对于SQL Server 2005 +)
Or (for SQL Server 2005+)
INSERT Mytable (col1, col2) OUTPUT INSERTED.IDCol VALUES (@param1, @param2);
这篇关于SQL INSERT命令后,在/返回标识列的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!