我正在制作一个基本的多项选择游戏。我有一个给定字段的现有数据库:Qno、Question、Correct、Wrong1、Wrong2 和 Wrong3。当表单加载时,此代码会从我的数据库中生成随机顺序的问题,将问题放置在标签上,并将选项放在相应的按钮上。
private void Form1_Load(object sender, EventArgs e)
{
NewQuestion();
public void NewQuestion()
{
Stack numberCheck = new Stack();
int y = 0;
Random x = new Random();
bool exists = false;
y = x.Next(1, 50);
exists = numberCheck.Contains(y);
while (exists == true)
{
y = x.Next(1, 50);
exists = numberCheck.Contains(y);
}
label2.Text = Convert.ToString(y);
da.SelectCommand = new SqlCommand("SELECT Question from MC WHERE QNo=@id", cs);
da.SelectCommand.Parameters.Add("@id", SqlDbType.Int).Value = label2.Text;
cs.Open();
label1.Text = Convert.ToString(da.SelectCommand.ExecuteScalar());
cs.Close();
da.SelectCommand = new SqlCommand("SELECT Correct from MC WHERE QNo=@id", cs);
da.SelectCommand.Parameters.Add("@id", SqlDbType.Int).Value = label2.Text;
cs.Open();
button2.Text = Convert.ToString(da.SelectCommand.ExecuteScalar());
cs.Close();
da.SelectCommand = new SqlCommand("SELECT Wrong1 from MC WHERE QNo=@id", cs);
da.SelectCommand.Parameters.Add("@id", SqlDbType.Int).Value = label2.Text;
cs.Open();
button3.Text = Convert.ToString(da.SelectCommand.ExecuteScalar());
cs.Close();
da.SelectCommand = new SqlCommand("SELECT Wrong2 from MC WHERE QNo=@id", cs);
da.SelectCommand.Parameters.Add("@id", SqlDbType.Int).Value = label2.Text;
cs.Open();
button4.Text = Convert.ToString(da.SelectCommand.ExecuteScalar());
cs.Close();
da.SelectCommand = new SqlCommand("SELECT Wrong3 from MC WHERE QNo=@id", cs);
da.SelectCommand.Parameters.Add("@id", SqlDbType.Int).Value = label2.Text;
cs.Open();
button5.Text = Convert.ToString(da.SelectCommand.ExecuteScalar());
cs.Close();
}
}
}
问题清单:
- 问题可以重复
- 答案列表与它们在数据库中声明的顺序相同,其中按钮 1 始终正确,其余按钮错误