简体   繁体   English

使用 C# 和数据库创建测验应用程序

[英]Create quiz application using C# and database

I just finished to make the add part of a quiz, the part where admin add questions for guest.我刚刚完成了测验的添加部分,即管理员为访客添加问题的部分。

When I clicked next button I just see the last question I added.当我点击下一步按钮时,我只看到我添加的最后一个问题。 Here is the code:这是代码:

SqlConnection con = new SqlConnection(@"Data Source=MARIA-PC;Initial Catalog=Account;Integrated Security=True");
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT * FROM [dbo].[QuestAdd] WHERE Class = '1'", con);
        SqlDataReader reader = null;
        reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            textBox2.Text = (reader["Question"].ToString());
            textBox3.Text = (reader["R1"].ToString());
            textBox4.Text = (reader["R2"].ToString());
            textBox5.Text = (reader["R3"].ToString());
            textBox6.Text = (reader["R4"].ToString());
            if (textBox7.Text == (reader["R_correct"].ToString()))
               point = point + 1;
        }
        con.Close();

My problem is That I don't know why I see just the last question althoug in the table I have more than one question.我的问题是我不知道为什么我只看到最后一个问题,尽管我有不止一个问题。

The datareader is looping through all the results and overwriting the textbox values for each row in the query.数据读取器循环遍历所有结果并覆盖查询中每一行的文本框值。

In the code above, every time you run this, only the final row retrieved will be displayed.在上面的代码中,每次运行时,只会显示检索到的最后一行。

You might be better retrieving the data and storing in a data structure and recoding the next button to use this.您可能最好检索数据并存储在数据结构中并重新编码下一个按钮以使用它。

eg例如

//Set up datatable with all questions

DataTable dt=new DataTable();
dt.column.Add("Question",typeof(string));
dt.column.Add("R1",typeof(string));
dt.column.Add("R2",typeof(string));
dt.column.Add("R3",typeof(string));
dt.column.Add("R4",typeof(string));
dt.column.Add("R_correct",typeof(int));

SqlConnection con = new SqlConnection(@"Data Source=MARIA-PC;Initial Catalog=Account;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM [dbo].[QuestAdd] WHERE Class = '1'", con);
SqlDataReader reader = null;
reader = cmd.ExecuteReader();
while (reader.Read())
{
  DataRow dr=dt.NewRow();
  dr["Question"] = (reader["Question"].ToString());
  dr["R1"] = (reader["R1"].ToString());
  dr["R2"] = (reader["R2"].ToString());
  dr["R3"] = (reader["R3"].ToString());
  dr["R4"]  = (reader["R4"].ToString());
  dr["R_correct"]  = (reader["R_correct"]);
  dt.Rows.Add(dr);

}
con.Close();

I've used a datatable here but you could easily use a list of objects as well.我在这里使用了数据表,但您也可以轻松使用对象列表。 The code above just adds the data from the query into a datatable and you can then, in your next button, write code that changes the row number.上面的代码只是将查询中的数据添加到数据表中,然后您可以在下一个按钮中编写更改行号的代码。

This is just a rough start but it should get you on the right track这只是一个艰难的开始,但它应该能让你走上正轨

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM