从数据库中随机选择问题 [英] ramdomly selecting questions from database

查看:60
本文介绍了从数据库中随机选择问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SqlConnection con = new SqlConnection(@"Data Source=ATTRES-3BC2A6\SQLEXPRESS;Initial Catalog=harsha;Integrated Security=True");
            SqlDataAdapter da = new SqlDataAdapter("select * from Exam )", con);
            da.Fill(ds, "Exam");
            dt = ds.Tables["Exam"];
            i = dt.Rows.Count;
            int k = 0;
            dr = dt.Rows[k];
            Label1.Text = dr[0].ToString();
            RadioButton1.Text = dr[1].ToString();
            RadioButton2.Text = dr[2].ToString();
            RadioButton3.Text = dr[3].ToString();
            RadioButton4.Text = dr[4].ToString();

            dr = dt.Rows[k + 1];
            Label2.Text = dr[0].ToString();
            RadioButton5.Text = dr[1].ToString();
            RadioButton6.Text = dr[2].ToString();
            RadioButton7.Text = dr[3].ToString();
            RadioButton8.Text = dr[4].ToString();

            dr = dt.Rows[k + 2];
            Label3.Text = dr[0].ToString();
            RadioButton9.Text = dr[1].ToString();
            RadioButton10.Text = dr[2].ToString();
            RadioButton11.Text = dr[3].ToString();
            RadioButton12.Text = dr[4].ToString();

            dr = dt.Rows[k + 3];
            Label4.Text = dr[0].ToString();
            RadioButton13.Text = dr[1].ToString();
            RadioButton14.Text = dr[2].ToString();
            RadioButton15.Text = dr[3].ToString();
            RadioButton16.Text = dr[4].ToString();
            
            dr = dt.Rows[k + 4];
            Label5.Text = dr[0].ToString();
            RadioButton17.Text = dr[1].ToString();
            RadioButton18.Text = dr[2].ToString();
            RadioButton19.Text = dr[3].ToString();
            RadioButton20.Text = dr[4].ToString();



所以我有四个问题,数据库中也有四个问题.
每当我在其他时间登录时,我都会被打乱.
该代码部分正确吗?还是应该对查询进行一些更改?
请建议我.



So I have four questions and I have four questions in my database also.
Whenever I login different time I should get shuffled question.
Is this code part correct? Or should I make some changes in the query?
Please suggest me.

推荐答案

尝试一下:

Try This:

Random randNum;
Random oRandom = New Random(System.DateTime.Now.Millisecond);
Int MaxQue;
MaxQue=50;
RandNum = oRandom.Next(0, MaxQue);



在这里,RandNum可以作为您问题的ID.
您可以使用它从数据库中选择问题.

-谢谢.



Here the RandNum can be your Question''s ID.
And you can use it for selecting question from database.

-Thanks.


在按钮单击事件中,编写以下代码:

在此之前创建static int i = 0;在全局声明部分中;

In button click event write the following code:

Before that create static int i=0; in the global declaration part;

{
SqlConnection con = new SqlConnection(@"Data Source=ATTRES-3BC2A6\SQLEXPRESS;Initial Catalog=harsha;Integrated Security=True");
                  SqlDataAdapter da = new SqlDataAdapter("select * from Exam )", con);
                  da.Fill(ds, "Exam");
                  dt = ds.Tables["Exam"];
                  i = dt.Rows.Count;
                  
                  dr = dt.Rows[i];
                  Label1.Text = dr[0].ToString();
                  RadioButton1.Text = dr[1].ToString();
                  RadioButton2.Text = dr[2].ToString();
                  RadioButton3.Text = dr[3].ToString();
                  RadioButton4.Text = dr[4].ToString();
i++;
}



对于每次点击,您都会从数据库中得到一个不同的问题.

希望这对您有用.



For each click you will get a differnt question from your database.

Hope this is useful for you.


这篇关于从数据库中随机选择问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆