本文介绍了C#最后插入的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用这个SQL存储过程:
INSERT INTO [dbTblUsers]([strUserName中],[strPassword ])值(@ P1,P2 @); SELECT @@ IDENTITY;和调用过程:
//将新用户 NID = daUsers.InsertQuery(textBoxUsername.Text,textBoxPassword.Text); //插入新的Twitter的OAuth daTwitterOAuth.Insert(NID,textBoxConsumerKey.Text,textBoxConsumerSecret.Text,textBoxToken.Text,textBoxTokenSecret.Text);
我如何把对象@@ IDENTITY的int INT NID? 筛选
NID =(int)的daUsers.InsertQuery(textBoxUsername.Text,textBoxPassword.Text)?;解决方案
首先,不要使用 @@ IDENTTY - 使用 SCOPE_IDENTITY();第一个可以给你带来意想不到的答案,如果有任何涉及触发器。其次;这两个回报小数;投它在调用现场,IMO:
SELECT CAST(SCOPE_IDENTITY()为int)
I'm using a stored procedure with this 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? Like this?
nId = (int)daUsers.InsertQuery(textBoxUsername.Text, textBoxPassword.Text);解决方案
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)
更多推荐
C#最后插入的ID
发布评论