添加行后获取唯一字段?

问题描述:

可能重复:
Retrieve identity value from inserted record's primary key添加行后获取唯一字段?

我有一个领域具有独特的领域(即我没有自动更新)称为ID。我正在使用此代码为其添加字段:

DB.Snippets.AddSnippetsRow(10, 0, Leaves, Coins, Date.Now, Language, Username, Description) 

我需要做的就是获取它添加的唯一字段。每当我在那里添加一个字段时,它就是+1,但是如何在没有hacky数据库循环的情况下获得该ID?

+0

什么是AddSnippetsRow?你的代码,我们预计*猜测*它做了什么? – gbn 2011-06-15 14:50:05

+0

它*不重要,因为它是一个存储过程吗?一个IDENTITY列或NEWID()? SQL Server 2000或SQL Server 2005+?该ID来自SQL,但您已发布*您的*更高级别的.net方法。 – gbn 2011-06-15 14:53:59

+0

然后它是一个重复http://*.com/questions/5962322/how-to-get-the-primary-key-of-the-last-row-inserted-into-the-table/5962432#5962432或http ://*.com/questions/5228780/how-to-get-last-inserted-id/5228819#5228819或http://*.com/questions/5212226/t-sql-returning-the-new-insert -identity-to-c/5212238#5212238 etc – gbn 2011-06-15 14:57:36

http://msdn.microsoft.com/en-us/library/ms190315.aspx

我想你会发现你需要那里。 @@ IDENTITY可能是正确的,如果你想查询它,或者scope_identity,如果你想从AddSnippetsRow返回值。第二个选项可能更干净,但都应该工作...