2011-07-28 63 views
0

我有測驗代碼,但不確定我如何計算數據庫中問題的總數。我知道我需要一個計數查詢,但我不知道在哪裏插入它。 這裏是我的代碼:獲取錶行數C#

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.OleDb; 
using System.Data.Sql; 
using System.Data.SqlClient; 

namespace Quiz_Test 
{ 
public partial class Form1 : Form 
{ 
public Form1() 
{ 
    InitializeComponent(); 
} 

String chosenAnswer, correctAnswer; 
DataTable table; 
int questionNumber; 

private void Form1_Load(object sender, EventArgs e) 
{ 
    string cnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Hannah\\Desktop\\QuizQuestions.accdb"; 

    OleDbConnection conGet = new OleDbConnection(cnString); 
    OleDbCommand cmdGet = new OleDbCommand(); 

    conGet.Open(); 

    cmdGet.CommandType = CommandType.Text; 
    cmdGet.Connection = conGet; 

    cmdGet.CommandText = "SELECT * FROM QuizQuestions ORDER BY rnd()"; 

    OleDbDataReader reader = cmdGet.ExecuteReader(); 
    table = new DataTable(); 
    table.Load(reader); 

    foreach (DataRow row in table.Rows) 
    { 
    labelQuestion.Text = table.Rows[0]["Question"].ToString(); 
    radioButton1.Text = table.Rows[0]["Answer 1"].ToString(); 
    radioButton2.Text = table.Rows[0]["Answer 2"].ToString(); 
    radioButton3.Text = table.Rows[0]["Answer 3"].ToString(); 
    radioButton4.Text = table.Rows[0]["Answer 4"].ToString(); 

    correctAnswer = table.Rows[0]["Correct Answer"].ToString(); ; 
    questionNumber = 0; 
    } 
    conGet.Close(); 

} 

private void btnGoToNextOne_Click(object sender, EventArgs e) 
{ 
    String cnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Hannah\\Desktop\\QuizQuestions.accdb"; 

    OleDbConnection conGet = new OleDbConnection(cnString); 
    OleDbCommand cmdGet = new OleDbCommand(); 

    { 
    conGet.Open(); 

    cmdGet.CommandType = CommandType.Text; 
    cmdGet.Connection = conGet; 

    cmdGet.CommandText = "SELECT * FROM QuizQuestions ORDER BY rnd()"; 

    OleDbDataReader reader = cmdGet.ExecuteReader(); 
    reader.Read(); 

    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 if (radioButton4.Checked) 
    { 
     chosenAnswer = reader["Answer 4"].ToString(); 
    } 

    if (chosenAnswer == reader["Correct Answer"].ToString()) 
    { 

     labelQuestion.Text = table.Rows[questionNumber]["Question"].ToString(); 
     //and show possible answers: 
     radioButton1.Text = table.Rows[questionNumber]["Answer 1"].ToString(); 
     radioButton2.Text = table.Rows[questionNumber]["Answer 2"].ToString(); 
     radioButton3.Text = table.Rows[questionNumber]["Answer 3"].ToString(); 
     radioButton4.Text = table.Rows[questionNumber]["Answer 4"].ToString(); 
     correctAnswer = table.Rows[questionNumber]["Correct Answer"].ToString(); 
     questionNumber++; 

    } 
    else 
    { 
     MessageBox.Show("That is not the correct answer"); 
    } 
    } 
} 

}}

我知道我需要把「從QuizQuestions SELECT COUNT(*)」,但我不知道我怎麼能確定的「位置」在一組問題,使我沒有得到這個錯誤:

IndexOutOfRangeException was unhandled 
There is no row at position 5 

回答

0

這是 「table.Rows [questionNumber]」,也就是殺了你,你需要做的在這一領域

table.Rows.Count檢查。類似於

if (questionNumber < table.Rows.Count) 
2

如果您已經在拉回所有的記錄規劃,你可以從DataTable中獲得的計數拉回紀錄後組。例如

_recordCount = table.Rows.Count; 

商店訪問到您的類,然後這個變量在範圍對證枚舉到下一個記錄,如前

if(questionNumber+1<=_recordCount) { 
    _recordCount++; 
} 
else 
{ 
    // No more questions, do something else here. 
} 

正如我剛纔注意到您table變量是私有定義,你也可以只檢查對table.Rows.Count直接,而不是存儲的變量。例如

if(questionNumber+1<=_table.Rows.Count) { 
    _recordCount++; 
} 
else 
{ 
    // No more questions, do something else here. 
} 
1

你只是在尋找table.Rows.Count? 我只查看了你的代碼,但是它看起來像是在你的foreach循環的每一次迭代中使用第一行(table.Rows [0])。