自动生成编号C# [英] auto generate number c#

查看:95
本文介绍了自动生成编号C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我需要您的帮助.我的参议员这样的人

我要在同一时间插入多个记录,以便用户同时点击.但是在这里我发现了从前端开始的运行编号(从数据库中获取最大编号并将该编号加1),然后在每一行中添加此编号并形成插入查询.


因此,在执行插入查询之前,如果其他人也获得了相同的数字,那么如何处理该数字.

致谢,

Hi All,

I need some help from u people.My senario like this

I am inserting multiple records at the same time users are hitting the same time. But here i found Running number from front end(Get the max number from database and adding 1 to that number) and adding this number in each and every row and forming insert query.


So before executing insert query if others are also getting the same number so how to handle this one.

Thanks & Regards,

推荐答案

只需在表中添加一个自动递增的整数ID字段即可.
Just add an integer ID field to the table that auto-increments.


是的,如John表示可以让您的数据库生成该编号,如果您需要在前端使用该编号,则只需在插入完成后让过程将其返回即可.

像...

Yes, as John says let you database generate this number, if you need to use the number in the front end then just have your procedure return it after the insert has completed.

Something like...

CREATE PROCEDURE  example_procedure

	(
		@SomeVariable	INT,
		@AnoherVariable	INT,
		@UniqueFieldID	INT OUTPUT

	)

AS

SET NOCOUNT ON

INSERT INTO
	TheTable (Field1, Field2)
VALUES
	(@SomeVariable, @AnoherVariable)

--Return the new identity
SET @UniqueFieldID  =  SCOPE_IDENTITY()

-- Return any error codes \ Reset the NOCOUNT property.
RETURN @@ERROR
SET NOCOUNT OFF
GO


您可以打开自动编号,也可以每次生成RANDOM NUMBER作为主键这对所有用户而言都是不同的.

无需获取最后一条记录并在其ID中添加1,而只需为所有记录插入随机数,即使它同时在数据库中输入,所有用户的记录也会有所不同.

(随机性取决于您编写的算法)

阿舒托什·贾因(Ashutosh Jain).
You can either turn the auto number on or you can also generate RANDOM NUMBER everytime as a primary key which will be diffrent for all the users.

No need to fetch the last record and add 1 in its id, instead just insert random number for all records which will be diffrent for all the users even if it wil be having entry in DB at same time.

(Random depends on the algorithm that you write)

Ashutosh Jain.


这篇关于自动生成编号C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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