C# 从数据库中随机选择不重复

本文关键字:选择 随机 数据库 | 更新日期: 2023-09-27 18:37:23

我有一个脚本,可以随机顺序从数据库中选择。

该脚本是自动考试的模拟。我想要的是,如果已经选择了问题,则不要再次提取。

我该怎么做?另外,如果这 3 个checkboxes中没有一个被选中,当出现消息框时,它会选择另一个问题。 Select()用数据库中的值初始化标签,我以加载形式使用它。

这是我到目前为止尝试过的代码:

private void select()
{
     string dataA = "SELECT * FROM questions order by rand()";
     MySqlCommand cmd = new MySqlCommand(dataA, index.connect);
     cmd.CommandType = CommandType.Text;
     using (index.connect)
     {
        index.connect.Open();
        MySqlDataReader rdr = cmd.ExecuteReader();
        try
        {
           if (rdr.Read())
           {
               label2.Text = rdr["question"].ToString();
               label2.AutoSize = true;
               label2.UseCompatibleTextRendering = true;
               label3.Text = rdr["answerswer1"].ToString();
               label4.Text = rdr["answerswer2"].ToString();
               label5.Text = rdr["answerswer3"].ToString();
               option1 = checkBox1.Checked;
               option2 = checkBox2.Checked;
               option3 = checkBox3.Checked;
            }
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
         }
         finally
         {
            index.connect.Close();
         }
      }
  }
private void button1_Click(object sender, EventArgs e) 
// in cazul in care raspunzi la o intrebare, 
//iar aceasta ramane, orice a-i raspunde, pune la incorect
{
    string dataA = "SELECT * FROM questions order by rand()";
    MySqlCommand cmd = new MySqlCommand(dataA, index.connect);
    cmd.CommandType = CommandType.Text;
    using (index.connect)
    {
        index.connect.Open();
        MySqlDataReader rdr = cmd.ExecuteReader();
        if (rdr.Read())
        {
            label2.Text = rdr["question"].ToString();
            label3.Text = rdr["answerswer1"].ToString();
            label4.Text = rdr["answerswer2"].ToString();
            label5.Text = rdr["answerswer3"].ToString();
            option1 = checkBox1.Checked;
            option2 = checkBox2.Checked;
            option3 = checkBox3.Checked;
            if (checkBox1.Checked == false && 
                checkBox2.Checked == false && 
                checkBox3.Checked == false)
            {
                MessageBox.Show("Bifati minim o casuta!");
                //imi selecteaza alta intrebare
                 return;
            }
            else
            {
               if ((option1.ToString() == rdr["option1"].ToString()) &&
                   (option2.ToString() == rdr["option2"].ToString()) && 
                   (option3.ToString() == rdr["option3"].ToString()))
               {
                    corect++;
                    label10.Text = corect.ToString();
                    checkBox1.Checked = false;
                    checkBox2.Checked = false;
                    checkBox3.Checked = false;
               }
               else
               {
                    incorect++;
                    label12.Text = incorect.ToString();
                    checkBox1.Checked = false;
                    checkBox2.Checked = false;
                    checkBox3.Checked = false;
               }
           }
       }
  }

C# 从数据库中随机选择不重复

您需要排除已选择的问题,以免返回给您。

为此,您需要将方法返回的每个问题的 id 存储在集合中,并使用该集合排除问题以供下次选择。

避免在数据库中使用标志,因为仅当只有一个应用程序实例正在运行时,该标志才有效。如果有多个实例,则每个实例将获得不同的问题,并且应仅排除其收到的问题。

举例来说:

public class Question
{
    public int Id {get; set;}
    public string Text {get; set;}
    public Answer[] Answers {get; set;}
}
public class QuestionSelector
{
    private readonly List<int> _previousQuestionIds = new List<int>();
    public Question NextQuestion()
    {
        var query = "select top 1 * from Questions ";
        if(ids.Any())
        {
            var ids = String.Join(",", _previousQuestionIds.Select(id=>id.ToString()));
            query += "  where id not in (" + ids + ") ";
        }
        query += " order by rand()";
        var question = ParseQuestion(query);
        _previousQuestionIds.Add(question.Id);
        return question;
    }
    private Question ParseQuestion(string query)
    {
        // query the database and convert the data from the returned row
    }
}

让上面的类在加载表单时创建一个实例,并调用NextQuestion()方法来加载下一个问题。它将从一个空的 id 集合开始,这意味着在第一次调用时它不会排除任何问题,但每次调用 NextQuestion() 方法时,它都会将返回的问题的 id 添加到列表中,在下一次调用时,问题将被排除。