views:

86

answers:

2

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,

+2  A: 

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);
Eoin Campbell
+1  A: 

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())
Mitch Wheat
I didnt get your code! I need to get the itemIDs as well as other values.
pier
So SELECT TOP N * FROM item_k ORDER BY CHECKSUM(NEWID()), no?!
Alex Martelli
Thank you! It worked fine. :)
pier