Skip to content

How to access random data from database

Ajay Kadian

Asked Mar 18, 2014, 3:37 AM EDT1 answer
Share
LinkedInX
Hi,
I have a MS access database. In that, one table consists of questions,option and answers.I need to retrieve random question from that table using Question Type but one by one it means at button click it shows one question on next click next question so on.
I have the code but it doesn't work.

And Please tell me it is possible to pass value One column value to textBox and 4 option column value to 4 button and if we click a button if its text value matches with answer(i.e in other column) then backcolor of button green else red.


cn.Open();
            OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\C#\\Database1.accdb");
            OleDbCommand cmd = new OleDbCommand("SELECT top 1 row Type from QUESTION ORDER BY rnd()", cn);
            
            OleDbDataAdapter adp = new OleDbDataAdapter(cmd);
            DataSet ds = new DataSet();
            adp = new OleDbDataAdapter("Select * from QUESTION", cn);
            adp.Fill(ds, "QUESTION");
            dataGridView1.DataSource = ds.Tables["QUESTION"];
            cn.Close();

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Hi Ajay Kadian,
    you can use random class to access random data from database. see the below code

     Random RD = new Random();
     RD.Next(TotalQuestion);
    and query data from databse using Questionnumber that mathces with random number i.s

    OleDbCommand cmd = new OleDbCommand("SELECT top 1 row Type from QUESTION WHERE QUSTION NO="+RD.Next(TotalQuestion)"+ORDER BY rnd()", cn);

    Kindly mark as answer if it solves you problem
    0