⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 selectquestionsform.cs

📁 老师信息管理
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MySchool
{
    /// <summary>
    /// 试题选择窗体
    /// </summary>
    public partial class SelectQuestionsForm : Form
    {
        public SelectQuestionsForm()
        {
            InitializeComponent();
        }

        // 窗体加载时,将科目从数据库中读取出来显示在组合框中
        private void SelectQuestionsForm_Load(object sender, EventArgs e)
        {
            bool error = false;  // 表示操作数据库是否出错

            string sql = "SELECT SubjectName FROM Subject";  // 查询用sql语句

            try
            {
                SqlCommand command = new SqlCommand(sql, DBHelper.connection);
                DBHelper.connection.Open();
                // 执行查询
                SqlDataReader dataReader = command.ExecuteReader();

                // 循环添加到组合框中
                while (dataReader.Read())
                {
                    cboSubjects.Items.Add(dataReader["SubjectName"].ToString());
                }
                dataReader.Close();
            }
            catch (Exception ex)
            {                
                error = true;
                Console.WriteLine(ex.Message);
            }
            finally
            {
                DBHelper.connection.Close();
            }

            if (error)  // 出错了
            {
                MessageBox.Show("数据库操作出错,请稍候再试!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // 放弃答题,退出应用程序
        private void btnGiveUp_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("还没有开始做题,真的要放弃吗?","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                this.Close();
            }
        }

        // 单击“开始答题”按钮
        private void btnBegin_Click(object sender, EventArgs e)
        {
            if (cboSubjects.SelectedIndex == -1)
            {
                MessageBox.Show("请选择科目!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                cboSubjects.Focus();                
            }
            else
            {
                // 获得选中科目的Id
                int subjectId = GetSubjectId(cboSubjects.Text);

                // 该科目问题总数
                int allQuestionCount = GetQuestionCount(subjectId);

                // 指定所有问题数组的长度                
                QuizHelper.allQuestionIds = new int[allQuestionCount];

                // 指定记录问题是否选中的数组的长度
                QuizHelper.selectedStates = new bool[allQuestionCount];

                // 为所有问题数组元素赋值
                SetAllQuestionIds(subjectId);

                // 抽题
                SetSelectedQuestionIds();

                // 取出标准答案
                SetRightAnswers();

                // 剩余时间为20分钟
                QuizHelper.remainSeconds = 1200;

                // 将学生答案数组初始化
                for (int i = 0; i < QuizHelper.studentAnswers.Length; i++)
                {
                    QuizHelper.studentAnswers[i] = "未回答";
                }

                // 打开答题窗体
                AnswerQuestionForm answerQuestion = new AnswerQuestionForm();
                answerQuestion.MdiParent = this.MdiParent;
                answerQuestion.Show();
                this.Close();
            }
        }

        // 获得对应科目的题目的总数
        private static int GetQuestionCount(int subjectId)
        {
            int questionCount = 0; // 该科目的题目总数
            string sql = "SELECT COUNT(*) FROM Question WHERE SubjectId=" + subjectId;
            try
            {
                SqlCommand command = new SqlCommand(sql, DBHelper.connection);
                DBHelper.connection.Open();
                questionCount = Convert.ToInt32(command.ExecuteScalar());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                DBHelper.connection.Close();
            }
            return questionCount;
        }

        // 获得科目的Id
        private int GetSubjectId(string subjectName)
        {
            int subjectId = -1;  // 科目的Id
            // 查询语句
            string sql = string.Format(
                "SELECT SubjectId FROM Subject WHERE SubjectName='{0}'",
                subjectName);
            try
            {
                SqlCommand command = new SqlCommand(sql, DBHelper.connection);
                DBHelper.connection.Open();
                subjectId = Convert.ToInt32(command.ExecuteScalar());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                DBHelper.connection.Close();
            }
            return subjectId;
        }

        // 获得某科目所有问题的Id
        private void SetAllQuestionIds(int subjectId)
        {
            string sql = "SELECT QuestionId FROM Question WHERE SubjectId="+subjectId;
            try
            {
                SqlCommand command = new SqlCommand(sql, DBHelper.connection);
                DBHelper.connection.Open();
                SqlDataReader dataReader = command.ExecuteReader(); // 执行查询
                
                // 为数组元素赋值
                for (int i = 0; i < QuizHelper.allQuestionIds.Length; i++)
                {
                    if (dataReader.Read())
                    {
                        // 为所有问题Id数组元素赋值
                        QuizHelper.allQuestionIds[i] = Convert.ToInt32(dataReader["QuestionId"]);                        
                        // 所有问题都是被选问题
                        QuizHelper.selectedStates[i] = false;
                    }
                }
                dataReader.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                DBHelper.connection.Close();
            }
        }

        // 抽取试题
        private void SetSelectedQuestionIds()
        {
            Random random = new Random();
            int questionIndex = 0;  // 随机产生的问题的索引值

            // 抽取每一道题并保存抽出的题目的Id
            int i = 0;  // 记录抽取了几道题
            while (i < QuizHelper.questionNum)
            {
                // 在所有题目的数量范围内抽题产生随机数
                questionIndex = random.Next(QuizHelper.allQuestionIds.Length); 
 
                if (QuizHelper.selectedStates[questionIndex] == false)  // 如果没有被选中过,可以选择
                {
                    QuizHelper.selectedQuestionIds[i] = QuizHelper.allQuestionIds[questionIndex];
                    QuizHelper.selectedStates[questionIndex] = true;
                    i++;
                } 
            }            
        }

        // 取出试题的标准答案
        private void SetRightAnswers()
        {
            try
            {
                string sql = "";  // 查询用sql语句
                SqlCommand command = new SqlCommand();
                command.Connection = DBHelper.connection;
                DBHelper.connection.Open();

                for (int i = 0; i < QuizHelper.selectedQuestionIds.Length; i++)
                {
                    sql = string.Format("SELECT Answer FROM Question WHERE QuestionId={0}",
                        QuizHelper.selectedQuestionIds[i]);
                    command.CommandText = sql;

                    // 为标准答案数组赋值
                    QuizHelper.correctAnswers[i] = command.ExecuteScalar().ToString();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                DBHelper.connection.Close();
            }            
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -