简体   繁体   English

c#last insert id

[英]c# last insert id

I'm using a stored procedure with this sql: 我正在使用这个sql的存储过程:

INSERT INTO [dbTblUsers]([strUsername], [strPassword])
VALUES (@p1,@p2);
SELECT @@IDENTITY;

And calling the procedure: 并调用该程序:

// 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);

How do I cast the object @@IDENTITY int the int nId? 如何将对象@@ IDENTITY转换为int nId? Like this? 像这样?

nId = (int)daUsers.InsertQuery(textBoxUsername.Text, textBoxPassword.Text);

Firstly, don't use @@IDENTTY - use SCOPE_IDENTITY() ; 首先,不要使用@@IDENTTY - 使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM