pier
pier

Reputation: 503

random values from db in c#

How can I retrive random random ItemIDs from the list of existing ItemIDs in ItemID column intge db, given below is the sqlcommand I've used.

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

thanks,

Upvotes: 0

Views: 1139

Answers (2)

Mitch Wheat
Mitch Wheat

Reputation: 300579

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())

Upvotes: 1

Eoin Campbell
Eoin Campbell

Reputation: 44278

Is the itemID column in the database, a contiguous list of numbers ?

If so, you can just do...

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

Upvotes: 2

Related Questions