获取表行计数C#

时间:2011-07-28 13:44:49

标签: c# ms-access

我有测验代码但不确定我如何计算数据库中的问题总数。我知道我需要一个计数查询,但我不知道在哪里插入它。 这是我的代码:

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

3 个答案:

答案 0 :(得分:2)

如果您已经计划撤回所有记录,则可以在撤回记录集后从DataTable获取计数。 e.g。

_recordCount = table.Rows.Count;

将此变量存储在您的类可访问的范围内,然后在枚举到下一条记录之前检查它,例如

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

正如我刚刚注意到您的table变量是私有定义的,您也可以直接检查table.Rows.Count,而不是存储变量。 e.g。

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

答案 1 :(得分:1)

你正在寻找table.Rows.Count吗? 我只看了你的代码,但看起来你正在使用第一行(table.Rows [0])进行foreach循环的每次迭代。

答案 2 :(得分:0)

这是“table.Rows [questionNumber]”即杀死你,你需要做一个

table.Rows.Count检查该区域。像

这样的东西
if (questionNumber < table.Rows.Count )