jeudi 28 juillet 2016

How to generate random numbers using C# that hasn't been listed in a database?

I'm working on an application that allows me to generate random ID numbers (6 integers) for employees but I would like to know how to generate these random numbers using C# and not duplicate the numbers that has been already used by another employee in the SQL database. Below is the code done in SQL:

declare @unique_UID varchar(6)

select @unique_UID = Right(ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)), 6)

while exists (select 1 from dbo.tblUserID 

   where @unique_UID = UserID_UID 
         and len(@unique_uid) = 6
         and @unique_UID <> @parm_User_Pin)(
         select @unique_UID = Right(ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)), 6)

`




Aucun commentaire:

Enregistrer un commentaire