2011-06-15 46 views
-1

可能重複:
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://stackoverflow.com/questions/5962322/how-to-get-the-primary-key-of-the-last-row-inserted-into-the-table/5962432#5962432或http ://stackoverflow.com/questions/5228780/how-to-get-last-inserted-id/5228819#5228819或http://stackoverflow.com/questions/5212226/t-sql-returning-the-new-insert -identity-to-c/5212238#5212238 etc – gbn 2011-06-15 14:57:36

回答

0

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

我想你會發現你需要那裏。 @@ IDENTITY可能是正確的,如果你想查詢它,或者scope_identity,如果你想從AddSnippetsRow返回值。第二個選項可能更乾淨,但都應該工作...

相關問題