Random values ​​from db in C #

How can I get random random ItemIDs from the list of existing ItemIDs in the intID column of the ItemID database, below is the sqlcommand code I used.

(SqlCommand RetrieveComm =new SqlCommand("SELECT * FROM item_k WHERE ItemID='" +intGetRequest+ "'", searchCon))

      

thanks,

0


a source to share


2 answers


You have not specified which RDBMS you are using.

If you are using SQL Server this will return N random rows:



SELECT TOP N    
    SomeColumn 
FROM     
    SomeTable
ORDER BY     
    CHECKSUM(NEWID())

      

+1


a source


Is a column itemID

in a database a continuous list of numbers?

If so, you can simply do ...



Random r = new Random();
int x = r.Next(1, MAX_ID_FROM_DB);

      

+2


a source







All Articles