ms access - c#测验代码不能正常工作

本文关键字:常工作 工作 不能 代码 access ms | 更新日期: 2023-09-27 18:02:05

我正在做一个测试,它正在工作到一个点,但在试图增加其复杂性之后,它不能完全工作,因为它应该。目前所发生的一切是,我的Access数据库表中的可能答案被绑定到我的c#表单上的每个单选按钮。

这部分是可以的,但是,点击我的按钮后,它不再告诉我我选择的答案是否正确。我现在正在使用一个标签来告诉用户答案是否正确。

下面是我的代码:
namespace WindowsFormsApplication1
{
    public partial class quizQuestions : Form
    {
        public quizQuestions()
        {
            InitializeComponent();
        }
        //int questionNumber;
        //String correctAnswer;
        private void WindowsAnalysisQuiz_Load(object sender, EventArgs e)
        {
            //declare connection string using windows security
            string cnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:''Users''Hannah''Desktop''quizQuestions.accdb";
            //declare Connection, command and other related objects
            OleDbConnection conGet = new OleDbConnection(cnString);
            OleDbCommand cmdGet = new OleDbCommand();
            //try
            //{
            //open connection
            conGet.Open();
            String correctAnswer;
            cmdGet.CommandType = CommandType.Text;
            cmdGet.Connection = conGet;
            cmdGet.CommandText = "SELECT * FROM quizQuestions ORDER BY rnd()"; // select all columns in all rows
            OleDbDataReader reader = cmdGet.ExecuteReader();
            reader.Read();
            label1.Text = reader["Question"].ToString();
            radioButton1.Text = reader["Answer 1"].ToString(); 
            radioButton2.Text = reader["Answer 2"].ToString();
            radioButton3.Text = reader["Answer 3"].ToString();
            radioButton4.Text = reader["Answer 4"].ToString();
            correctAnswer = reader["Correct Answer"].ToString();
            //questionNumber = 1;
            conGet.Close();
        }
        private void btnNextQuestion_Click(object sender, EventArgs e)
        {
            String cnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:''Users''Hannah''Desktop''quizQuestions.accdb";
            //declare Connection, command and other related objects
            OleDbConnection conGet = new OleDbConnection(cnString);
            OleDbCommand cmdGet = new OleDbCommand();
            //try
            {
                //open connection
                conGet.Open();
                cmdGet.CommandType = CommandType.Text;
                cmdGet.Connection = conGet;
                //cmdGet.CommandText = "SELECT * FROM quizQuestions ORDER BY rnd()"; // select all columns in all rows
                OleDbDataReader reader = cmdGet.ExecuteReader();
                reader.Read();
                String chosenAnswer = "";
                int chosenCorrectly = 0;
                if (radioButton1.Checked)
                {
                    chosenAnswer = reader["Answer 1"].ToString();
                }
                else if (radioButton2.Checked)
                {
                    chosenAnswer = reader["Answer 2"].ToString();
                }
                else if (radioButton3.Checked)
                {
                    chosenAnswer = reader["Answer 3"].ToString();
                }
                else
                {
                    chosenAnswer = reader["Answer 4"].ToString();
                }
                if (chosenAnswer == reader["Correct Answer"].ToString())
                {
                    //chosenCorrectly++;
                    label2.Text = "You have got this answer correct";
                    //label2.Text = "You have got " + chosenCorrectly + " answers correct";
                }
                else
                {
                    MessageBox.Show("That is not the correct answer");
                }
            }
        }
    }
}

那么总结一下,可能的答案被加载到表单OK中,但是当我按下表单上的按钮以确定是否选择了正确的答案时,什么都没有发生。

ms access - c#测验代码不能正常工作

您没有在代码中的任何地方绑定btnNextQuestion_Click,并且您没有在btnNextQuestion_Click函数中给出cmdGet.CommandText。你可能需要更具体地说明什么不会发生。你调试了吗?

如果确实"nothing"发生,那么您应该检查按钮的click事件是否仍然绑定到事件处理程序。

您已经注释掉了为查询设置CommandText的行,因此您正在尝试执行没有查询集的命令,这应该会给您一个异常。

我认为你得到的东西有严重的问题。您正在将答案分配给Text属性,并且已经在字符串中有正确的答案。

只是检查相同的检查RadioButton文本,你会知道。此外,很难猜测您在btnNextQuestion_Click事件中查询的内容,我有我的保留意见,因为它实际上是当前显示的相同问题,并由用户选择答案。