数据库那边没有问题,连接语句在别的窗体也能用,怀疑是questionIndex的问题,求大神帮忙看下怎么改
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Collections;
namespace graduationproject
{
public partial class PsychologicalTest : Form
{
public int questionIndex = 1;
public PsychologicalTest()
{
InitializeComponent();
}
private void PsychologicalTest_Load(object sender, EventArgs e)
{
timer1.Start(); // 启动计时器
GetQuestionDetails(); // 显示题目信息
CheckOption(); // 如果题目已经答过,让相应的选项选中
CheckBtnNext(); // 确定是否到了最后一题
}
private void CheckBtnNext()
{
// 如果达到20题,就让“下一题”按钮的文字显示为“提交”
if (questionIndex >= QuizHelper.selectedQuestionIds.Length)
{
button2.Text = "提交";
}
}
private void button2_Click(object sender, EventArgs e)
{
// 如果没有到最后一题,就继续显示新题目信息
if (questionIndex < QuizHelper.selectedQuestionIds.Length)
{
questionIndex++;
GetQuestionDetails(); // 显示试题信息
CheckOption(); // 如果题目已经答过,让相应的选项选中
CheckBtnNext(); // 确定是否到了最后一题
}
else // 否则,打开答题卡窗体
{
OpenResultCard();
}
}
private void OpenResultCard()
{
PsychologicalTestResult psychologicalTestResult = new PsychologicalTestResult();
psychologicalTestResult.MdiParent = this.MdiParent;
psychologicalTestResult.Show();
this.Close();
}
private void timer1_Tick(object sender, EventArgs e)
{
int minute; // 当前的分钟
int second; // 秒
// 如果还有剩余时间,就显示剩余的分钟和秒数
if (QuizHelper.remainSeconds > 0)
{
QuizHelper.remainSeconds--;
minute = QuizHelper.remainSeconds / 60;
second = QuizHelper.remainSeconds % 60;
label4.Text = string.Format("{0:00}:{1:00}", minute, second);
}
// 否则,就提示交卷
else
{
timer1.Stop();
MessageBox.Show("时间到了!", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
this.Close();
}
}
private void radioButton_Click(object sender, EventArgs e)
{
QuizHelper.studentAnswers[questionIndex] = Convert.ToString(((RadioButton)sender).Tag);
}
public void GetQuestionDetails()
{
// 显示当前的题目信息
label1.Text = string.Format("第{0}题:", questionIndex);
// 查询题目信息的sql语句
string sql = "SELECT Question FROM PsyQuestion WHERE QNumber=questionIndex ";
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=graduationproject;Integrated Security=true");
try
{
conn.Open();
SqlCommand command = new SqlCommand(sql, conn);
SqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
label3.Text = reader["Question"].ToString();
}
reader.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
DBHelper.connection.Close();
}
}
private void CheckOption()
{
switch (QuizHelper.studentAnswers[questionIndex])
{
case "A":
radioButton1.Checked = true;
break;
case "B":
radioButton2.Checked = true;
break;
case "C":
radioButton3.Checked = true;
break;
default:
radioButton1.Checked = false;
radioButton2.Checked = false;
radioButton3.Checked = false;
break;
}
}
}
}