954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Random Records From Access Database

Hey I am trying to do a project on Test Engine. I need to retrieve random rows of questions from access database. I did the following code, but every time I once close and run it again, the sequence of questions is same. How can I change the sequence every time?

private void button1_Click(object sender, EventArgs e)
        {
            OleDbConnection con = new OleDbConnection(Connection.conStr);
            OleDbCommand cmd = new OleDbCommand("select * from dell order by Rnd(serialNumber)", con);
            OleDbDataAdapter adp = new OleDbDataAdapter(cmd);
            DataSet ds = new DataSet();
            adp.Fill(ds, "new");
            dataGridView1.DataSource = ds.Tables["new"];
        }
guru_iyer
Light Poster
30 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

SELECT *, Rnd(serialNumber) FROM dell ORDER BY Rnd(serialNumber)

Momerath
Nearly a Senior Poster
3,384 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 

check this link it contains a detailed discussion on how to randomly select values

abelLazm
Postaholic
2,113 posts since Feb 2011
Reputation Points: 219
Solved Threads: 124
 
Saikalyankumar
Junior Poster in Training
91 posts since Mar 2011
Reputation Points: 8
Solved Threads: 23
 

Use any of these Queries

SELECT *, rand(@@IDLE % (@@ROWCOUNT + serialNumber))as xx FROM dell ORDER BY xx
//or
SELECT *,(sin(serialNumber * rand())) as xx from dell
ORDER BY xx
abelLazm
Postaholic
2,113 posts since Feb 2011
Reputation Points: 219
Solved Threads: 124
 

@abelLazm Thanks a lot.. It worked..

guru_iyer
Light Poster
30 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: