如何在asp.net中创建唯一的自动增量ID号 [英] How to create unique auto increment id number in asp.net

查看:339
本文介绍了如何在asp.net中创建唯一的自动增量ID号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张桌子



1)taskheader

2)taskdetails



taskheader列



sno,任务名号



taskdetails专栏



任务编号,受让人



在taskheader sno列中是自动增量,在taskdetails中tasknumber列有

到作为任务主管的sno。



两张桌子都必须单击才能填充。



我的问题是如何获取tasknumber表单taskheader表

i have two tables

1)taskheader
2)taskdetails

taskheader columns

sno,taskname

taskdetails columns

tasknumber,assignee

in the taskheader sno column is auto increment and in taskdetails tasknumber column has
to be sno of taskheader.

both tables have to be populated with single click.

my problem is how to get the tasknumber form taskheader table

推荐答案

假设您使用数据库,最好的方法是编写一个存储过程来执行它 - 并包含整个操作在交易中。

然后,在交易中你可以使用 @@ IDENTITY [ ^ ]以便插入第一个表中的e值,并将其插入第二个表格。



但是我自己,我不会使用Identity值 - 我会使用Guid相反,并从C#代码传递它。
Assuming you use a database, the best way is probably to write a stored procedure to do it - and enclose the whole operation in a transaction.
Then, within the transaction you can use @@IDENTITY[^] to fecth the value inserted in the first table, and insert it in the second.

But myself, I wouldn't use Identity values for this - I'd use a Guid value instead, and pass that through from the C# code.


有几种方法可以获取自动生成的值的最后插入值,例如标识或自动增量。一个简单的方法是使用自然键重新查询数据或使用函数来获取最新值等等。



但是,我更喜欢使用 OUTPUT子句 [ ^ ]。通过这种方式,您可以在同一语句中轻松执行INSERT并获取值,而无需额外的往返。
There are several ways of getting the last inserted value for auto generated values such as identity or auto-increment. One simple is to requery the data using the natural key or to use function to get the latest value and so on.

However, I would prefer using OUTPUT clause[^]. This way you can do the INSERT and get the value easily in the same statement without extra roundtrips.


这篇关于如何在asp.net中创建唯一的自动增量ID号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆