我的应用程序中的ADO.NET问题. [英] ADO.NET issue in my application.

查看:84
本文介绍了我的应用程序中的ADO.NET问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我有3个问题,每个问题有4个选项.我正在使用标签"和单选按钮"作为答案.

我正在尝试使用ADO.NET从这些控件动态获取值.

但是我无法这样做,因为我无法正确地遍历表.对于问题和选择,我只能读取一行.

我正在维护2个表,一个用于问题,另一个用于选择.

这是文件后面代码中的代码.

Dear All,

I am having 3 questions and for each question 4 options. I am using Labels and Radio Buttons for the answers.

I am trying to fetch the values from these controls dynamically using ADO.NET.

But I am unable to do so, as I am not able to iterate through the tables properly. I am able to fetch only a single row for both the question as well as the choices.

I am maintaining 2 tables, 1 for the question and another for the choices.

Here is the code in the code behind file.

SqlConnection conn = new SqlConnection(ConnectionString);
        conn.Open();
        string add_questions = "select * from table1";
        string add_choices = "select * from table2";

        SqlCommand cmd = new SqlCommand(add_questions, conn);
        SqlCommand cmd1 = new SqlCommand(add_choices, conn);

        SqlDataReader read_questions = cmd.ExecuteReader();
        //SqlDataReader read_answers = cmd1.ExecuteReader();

        if(read_questions.HasRows)
        {
            read_questions.Read();
            for (int i = 0; i < 2; i++)
            
            {
                q1.Text = read_questions.GetString(i);
               //q1 is the id of the Label1. Similarly there are 2
               //other labels.    
                
            }

        }
        read_questions.Close();

        SqlDataReader read_answers = cmd1.ExecuteReader();

        if (read_answers.HasRows)
        {
            read_answers.Read();
            for (int i = 0; i < 3; i++)
            {
                RadioButton1.Text = read_answers.GetString(i);
            }
        }



仅显示第一个问题和第一个选项.

请协助

感谢和问候



Only the 1st question and the first option is displayed.

Kindly assist

Thanks and Regards

推荐答案

您的代码有很多问题,但是导致仅显示一个问题和一个答案的主要原因是

在for循环中,您仅将值分配给一个控件

像这样q1.text = ....和RadioButton1.Text = ....

因此,每次循环迭代时,您的代码都会为其分配最后一个值.所以最后您只会看到一个问题和一个答案.

您需要使用RadioButtonList并使用此属性来拉出所有问题.

RadioButtonList1.DataSource = reader;
your code has many problems but one main reason that is causing to show only one question and one answer is

in your for loop you are assigning values to just ONE control

like this q1.text = .... and RadioButton1.Text=....

so every time loop iterates, your code assign last value to it. so at end you will see only one question and one answer.

you need to use RadioButtonList and use this property to pull all questions.

RadioButtonList1.DataSource = reader;


您似乎有大量的知识来学习使用ADO.NET和数据库设计.

在您的代码中,您仅获得一条记录,因为这就是您所要的.您只需致电一次即可.

You have a considerable amount to learn about using ADO.NET and database design in general it seems.

In your code you are only getting one record because that is all you are asking for. You only call Read once.

using(SqlConnection conn = new SqlConnection(...))
{
  using(SqlCommand cmdQ = new SqlCommand("...", conn))
  using(SqlCommand cmdA = new SqlCommand("...", conn))
  {
    SqlDataReader readerQ = cmdQ.ExecuteReader();
    while(readerQ.Read())
    {
      ....
    }
  }
}



您永远不要在select语句中使用*,因为它不会给SQL Server查询引擎带来更改以优化查询和返回流.它还根本不记录您的代码.别人怎么知道,或者您还记得将来会返回什么?

问题和答案之间也应该有父母/孩子的关系.然后使用 DataRelation [



You should never use * in a select statement as it does not give SQL Server query engine a change to optimize the query and return stream. It also does not document your code at all. How will someone else know, or you remember in the future, what is being returned?

There should also be a parent/child relationship between the questions and answers. Then using DataRelation[^] you can query a DataSet for related records.


1.这些表之间没有关系(父子关系).
2.必须使用SqlDataTable和SqlAdapter来获取数据,这是一个更好的选择.

1. There is no relation between this tables (parent child relation).
2. you must use SqlDataTable and SqlAdapter to fetch data , it is a much bettter .

SqlDataConnection cnn = new .....;
SqlCommand quesCmnd = cnn.CreateCommand();
SqlCommand answrCmnd = cnn.CreateCommand();

quesCmnd.ComandText = "select * from table1";       
answrCmnd.CommandText = "select * from table2";

DataTable quesDt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(quesCmnd);
adapter.Fill(quesDt);

DataTable answrDt = new DataTable();
adapter = new SqlDataAdapter(answrCmnd);
adapter.Fill(answrDt);

foreach(DataRow dr in quesDt.Rows)
{
// do some operation you want 
//dr["ColumnName"];
//dr[ColumnIndex];
}



您可以像quesDt一样在answrDt上执行foreach循环,而不是DataReader.
但重要的是这里的表之间没有正确的关系.



you can do foreach loop on answrDt like quesDt instead of DataReader.
but important thing in here is that your tables have not correct relation to each other.


这篇关于我的应用程序中的ADO.NET问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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