Dear Friends
i want to create dynamic id on base of datacolumn .So tell me Operation should do in database or C# code .which will be best .
Operation should prefer in database or c# code
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.


Khargesh RajputRank #616 · 3.9K reputationPosted Apr 23, 2015, 10:44 PM EDT
Random r = new Random();
// Always 5, 6, 7, 8 or 9.
Console.WriteLine(r.Next(5, 10));
Console.WriteLine(r.Next(5, 10));
Console.WriteLine(r.Next(5, 10));
for sql
DECLARE @Upper INT; DECLARE @Lower INT;
SET @Lower = 1 -- The lowest random number
SET @Upper = 49 -- The highest random number
SELECT @Lower + CONVERT(INT, (@Upper-@Lower+1)*RAND())
To get random number
without repetition I think this will do the job
;with CTE as ( SELECT randomNumber, COUNT(1) countOfRandomNumber FROM ( SELECT ABS(CAST(NEWID() AS binary(6)) %49) + 1 randomNumber FROM sysobjects) sample GROUP BY randomNumber ) SELECT TOP 5 randomNumber FROM CTE ORDER BY newid()