本文介紹了c# 最后插入id的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
限時送ChatGPT賬號..
我正在使用這個 sql 的存儲過程:
I'm using a stored procedure with this sql:
INSERT INTO [dbTblUsers]([strUsername], [strPassword])
VALUES (@p1,@p2);
SELECT @@IDENTITY;
并調(diào)用程序:
// Insert new user
nId = daUsers.InsertQuery(textBoxUsername.Text, textBoxPassword.Text);
// Insert new Twitter OAuth
daTwitterOAuth.Insert(nId, textBoxConsumerKey.Text, textBoxConsumerSecret.Text, textBoxToken.Text, textBoxTokenSecret.Text);
如何將對象 @@IDENTITY 轉(zhuǎn)換為 int nId?像這樣?
How do I cast the object @@IDENTITY int the int nId? Like this?
nId = (int)daUsers.InsertQuery(textBoxUsername.Text, textBoxPassword.Text);
推薦答案
首先,不要使用@@IDENTTY
- 使用SCOPE_IDENTITY()
;如果涉及任何觸發(fā)器,第一個可以給你意想不到的答案.其次;這兩個都返回小數(shù);將其投射到呼叫站點(diǎn),IMO:
Firstly, don't use @@IDENTTY
- use SCOPE_IDENTITY()
; the first can give you unexpected answers if there are any triggers involved. Secondly; both of these return decimals; cast it at the call-site, IMO:
SELECT CAST(SCOPE_IDENTITY() as int)
這篇關(guān)于c# 最后插入id的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!