2011-07-28 6 views
0

Je code pour un quiz, mais je suis incertain sur la façon dont je compte le nombre total de questions dans ma base de données. Je sais que j'ai besoin d'une requête de compte, mais je ne sais pas où l'insérer. Voici mon code:Obtenir la table Nombre de lignes 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"); 
    } 
    } 
} 

}}

Je sais que je dois mettre en "SELECT COUNT (*) à partir QuizQuestions" mais je ne sais pas comment je peux déterminer la 'position' dans l'ensemble des questions afin que je ne comprends pas cette erreur:

IndexOutOfRangeException was unhandled 
There is no row at position 5 

Répondre

0

Il est le "table.Rows [questionNumber]" qui vous tue, vous devez faire une

check table.Rows.Count dans ce domaine. Quelque chose comme

if (questionNumber < table.Rows.Count) 
2

Si vous êtes déjà sur la planification tirant tous les enregistrements, vous pouvez simplement obtenir un compte de la DataTable après avoir ramené le dossier ensemble. par exemple.

_recordCount = table.Rows.Count; 

magasin cette variable à périmètre accessible à votre classe, puis vérifiez contre avant énumérait l'enregistrement suivant, par exemple

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

Comme je viens de remarquer que votre table variable est définie en privé, vous pouvez aussi simplement vérifier directement contre la table.Rows.Count, au lieu de stocker une variable. par exemple.

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

êtes-vous simplement à la recherche de table.Rows.Count? J'ai seulement regardé votre code, mais il semble que vous utilisiez la première ligne (table.Rows [0]) pour chaque itération de votre boucle foreach.

Questions connexes