C#最后插入的ID

编程入门 行业动态 更新时间:2024-10-26 10:37:23
本文介绍了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

本文发布于:2023-10-13 20:43:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1489022.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!