从sqlserverdatabase检索数据 [英] Retrieve data from sqlserverdatabase

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

问题描述

我正在进行考试。所以我从数据库中检索问题。我得到的总数是10,我在网络表单中创建了10个radiobuttonlist,所以我从数据库中检索数据并将其添加到添加到表单中的10个radiobuttonlist。但如果数据库中的问题计数相应地动态增加如何自动/动态创建radiobuttonlist并将其绑定到数据库值:(请你帮帮我:(这是我的项目,:(





我的查询看起来像



I am conducting exam . So i retrieve questions from the database. i get the total count of the it is 10 , i have created ten radiobuttonlist in the web form so i retrieved data from database and added it to the ten radiobuttonlist that was added to the form . but if the question count in the database is increasing dynamically accordingly how to automatically /dynamically create radiobuttonlist and bind it to the database values :( can u please help me out :( this is my project , :(


my query looks like

SqlCommand cmd = new SqlCommand("select ROWNUMBER,questionno,tname,question,ans1,ans2,ans3,ans4 from questions where tname=''Prelim'' and questionno=''" + q + "''", con);
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               DataSet ds = new DataSet();
               da.Fill(ds, "questions");

               DataRow DR = ds.Tables[0].Rows[0];

               DataTable d = new DataTable();

               labell.Text = Session["New"].ToString();
               Question.Text = totalQs.ToString();
               sno = DR[1].ToString();

               TestName.Text = DR[2].ToString();

               for (int i = 1; i <= 10; i++)
               {

                   SqlCommand cmd1 = new SqlCommand("select ans1,ans2,ans3,ans4,cans from questions where tname=''Prelim'' and questionno=''" + i + "''", con);
                   SqlDataAdapter daa = new SqlDataAdapter(cmd1);
                   DataSet dss = new DataSet();
                   daa.Fill(dss, "questions");

                   DataRow DRR = dss.Tables[0].Rows[0];

                   if (i == 1)
                   {
                       RadioButtonList1.Items.Clear();
                       RadioButtonList1.Items.Add(DRR[0].ToString());
                       RadioButtonList1.Items.Add(DRR[1].ToString());
                       RadioButtonList1.Items.Add(DRR[2].ToString());
                       RadioButtonList1.Items.Add(DRR[3].ToString());
                   }
                   else if (i == 2)
                   {
                       RadioButtonList2.Items.Clear();
                       RadioButtonList2.Items.Add(DRR[0].ToString());
                       RadioButtonList2.Items.Add(DRR[1].ToString());
                       RadioButtonList2.Items.Add(DRR[2].ToString());
                       RadioButtonList2.Items.Add(DRR[3].ToString());
                   }
                   else if (i == 3)
                   {



等等





但是那些radiobuttonlist1,radiobuttonlist2都是以限制形式预定义/创建的:(


so on


But those radiobuttonlist1,radiobuttonlist2 are all predefined/created in the form that is the limitation :(

推荐答案

这里的更新版本不是你想要的,但for循环有动态创建radiobuttonlists的逻辑,其中Form1是你的表单名称。如果你需要更多的帮助,请问。
Here is the updated version that is not fully what you want, but the for loop has the logic to create radiobuttonlists dynamically where "Form1" is the name of your form. If you need more help, ask.
        SqlCommand cmd1 = new SqlCommand("select ans1,ans2,ans3,ans4,cans from questions where tname=''Prelim'' and questionno=''" + i + "''", con);
        SqlDataAdapter daa = new SqlDataAdapter(cmd1);
        DataSet dss = new DataSet();
        daa.Fill(dss, "questions");

        DataRow DRR = dss.Tables[0].Rows[0];

for (int i = 0; i <= dss.Tables[0].Rows.Count; i++)
    {
        RadioButtionList rbl = new RadioButtonList();
        rbl.ID = "rbl" + i;


        if (i == 1)
        {
            rbl1.Items.Clear();
            rbl1.Items.Add(DRR[0].ToString());
            rbl1.Items.Add(DRR[1].ToString());
            rbl1.Items.Add(DRR[2].ToString());
            rbl1.Items.Add(DRR[3].ToString());
            Form1.Controls.Add(rbl1);
        }
        else if (i == 2)
        {
            rbl2.Items.Clear();
            rbl2.Items.Add(DRR[0].ToString());
            rbl2.Items.Add(DRR[1].ToString());
            rbl2.Items.Add(DRR[2].ToString());
            rbl2.Items.Add(DRR[3].ToString());
            Form1.Controls.Add(rbl2);
        }


我建​​议您在asp.net表单上使用转发器,然后构建转发器组件来处理所有针对不同问题的不同设置。即。一个或多个答案的问题,但每个答案的答案数量不一定相同。

数据中继器控制 [ ^ ]
I would suggest that you use a repeater on your asp.net form and then build the repeater component to handle all the different setups for different questions. ie. a question with 1 or more answers, but not necessarily the same number of answers for each.
Data Repeater Control[^]


为什么你没有使用转发器控制?使用转发器控制将单选按钮放入其中并从数据集中的数据库中检索数据,并将该数据集作为数据源分配给转发器,这样它将自动生成等于数据集中记录的数字单选按钮...
Why you are not using repeater control? use repeater control put radio button in it and retrieve data from database in dataset and just assign that dataset as a datasource to repeater, so it will automatically generate number radio button equal to record in your dataset...

这篇关于从sqlserverdatabase检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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