为什么我的代码永远不会进入我的while循环?

时间:2017-03-11 13:54:02

标签: c# while-loop

我正在开展一项测验计划。

以下代码采用Form2_Load方法。

ctrque[]是全局integer变量。

你能帮我解决一下为什么代码没有进入while(read.Read())循环吗?

con.Open();
com.CommandText = "SELECT * FROM questions";
OleDbDataReader read = com.ExecuteReader();

while (read.Read()) { // this is the loop I'm talking about
    if (int.Parse(read["qid1"].ToString()) == que[ctr]) { // where que[] is containing of 30 unique random numbers
        qtnTxt.Text = read["qtn"].ToString(); //For output of the question
        for (int x = 0; x < 4; x++) { //for unique random choices in quiz bee. I have no problem about this.
            ans[x] = ran.Next(2, 6);
            for (int y = x; y >= 0; y--) {
                if (x == y) {
                    continue;
                } else if (ans[x] == ans[y]) {
                    ans[x] = ran.Next(2, 6);
                    y = x;
                }
            }
        }
    //Choices outputs
    aBtn.Text = read.GetString(ans[0]).ToString();
    bBtn.Text = read.GetString(ans[1]).ToString();
    cBtn.Text = read.GetString(ans[2]).ToString();
    dBtn.Text = read.GetString(ans[3]).ToString();
    }
}
ctr++;

read.Close();
com.ExecuteNonQuery();
con.Close();

1 个答案:

答案 0 :(得分:-1)

com.CommandText = "SELECT * FROM questions";移到OleDbDataReader read = com.ExecuteReader();

下方

并且有答案。我只是在我制作的一些应用程序上试过它,这是我的结论。

相关问题