C# 在不知道任何记录的情况下,为每一行单击循环表

C# 在不知道任何记录的情况下,为每一行单击循环表,c#,ms-access,C#,Ms Access,每次单击按钮时,我都会尝试在表格中循环。目前,我已将其设置为递增一个计数器,然后仅获取计数器编号的id匹配。但这不是一个好方法,因为我发现表中的ID列不一致。在ID 2之后,它跳到5,因此它变为1-2-5-6-7等。因此,当计数器为3时,它失败,因为ID为3的行不存在 是否有一种方法可以在不知道ID的情况下递增。只获取所有行数据 private void nextQuestion_Click(object sender, EventArgs e) { //

每次单击按钮时,我都会尝试在表格中循环。目前,我已将其设置为递增一个计数器,然后仅获取计数器编号的id匹配。但这不是一个好方法,因为我发现表中的ID列不一致。在ID 2之后,它跳到5,因此它变为1-2-5-6-7等。因此,当计数器为3时,它失败,因为ID为3的行不存在

是否有一种方法可以在不知道ID的情况下递增。只获取所有行数据

        private void nextQuestion_Click(object sender, EventArgs e)
    {
        //Setup Connection to access db
        string cnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Adam\\Desktop\\braithwaite.mdb";

        //declare Connection, command and other related objects
        OleDbConnection conGet = new OleDbConnection(cnString);
        OleDbCommand cmdGet = new OleDbCommand();

            //open connection
            conGet.Open();
            //String correctAnswer;

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

            cmdGet.CommandText = "SELECT * FROM SAM_IG_Questions WHERE ID = @counter";

            cmdGet.Parameters.AddWithValue("@counter", counter.Text);

            OleDbDataReader reader = cmdGet.ExecuteReader();

            reader.Read();

            questionNumber.Text = reader["QuestionName"].ToString();
            iqquestion.Text = reader["Description"].ToString();
            if (reader["Option1"] != DBNull.Value) { radioButton1.Text = reader["Option1"].ToString(); radioButton1.Show(); }
            else { radioButton1.Hide(); }
            if (reader["Option2"] != DBNull.Value) { radioButton2.Text = reader["Option2"].ToString(); radioButton2.Show(); }
            else { radioButton2.Hide(); }
            if (reader["Option3"] != DBNull.Value) { radioButton3.Text = reader["Option3"].ToString(); radioButton3.Show(); }
            else { radioButton3.Hide(); }
            if (reader["Option4"] != DBNull.Value) { radioButton4.Text = reader["Option4"].ToString(); radioButton4.Show(); }
            else { radioButton4.Hide(); }
            correctanswer.Text = reader["Answere"].ToString();
            instructions.Text = reader["Instructions"].ToString();
            correctInstructions.Text = reader["Instructions"].ToString();

            //questionNumber = 0;

            conGet.Close();
            //End Connection to access db
            panel1.Visible = true;
            iqresult.Visible = false;
    }
把这句话留在哪儿

cmdGet.CommandText = "SELECT * FROM SAM_IG_Questions";
然后用鼠标在你的行中循环

while(reader.Read())
{
    //handle row data  
}
编辑:示例如何使用OleDbDataReader。Reader方法:

Edit2:现在我得到了您想要实现的目标,您并不是试图循环遍历一个表,而是希望得到下一行。在这种情况下,您需要选择单行,存储加载行的id,并在单击时请求下一行。代码应该如下所示:

cmdGet.CommandText = "SELECT TOP 1 * FROM SAM_IG_Questions WHERE ID > @id" ORDER BY ID;
cmdGet.Parameters.AddWithValue("@counter", this.lastId);
...
reader.Read();
this.lastId = (int) reader["ID"]; 

questionNumber.Text = reader["QuestionName"].ToString();
...
// read other data as usual
this.lastId应在构造函数中初始化为-1,Try:cmdGet.CommandText=select*from select*,order by Id上的行数为RN from SAM\u IG\u Questions as Ellen,其中RN=@counter

这将生成与Id列值顺序相同的序列号RN列,然后仅选择行号与计数器匹配的行

要在SQL Server中测试此功能,请执行以下操作:

-- Create a sample table.
declare @SAM_IG_Questions as Table ( Id Int, Option1 Int, Option2 Int );
-- Load some sample data and display it.
insert into @SAM_IG_Questions ( Id, Option1, Option2 ) values
  ( 1, 0, 0 ), ( 2, 1, 0 ), ( 5, 1, 1 ), ( 6, 0, 1 ), ( 7, -1, -1 );
select * from @SAM_IG_Questions;
-- Display data with row numbers.
select *, Row_Number() over ( order by Id ) as RN FROM @SAM_IG_Questions;
-- Get a specific row by row number.
declare @Counter as Int = 3; -- Third row reading in order of increasing   Id   values.
select * from
  ( select *, Row_Number() over ( order by Id ) as RN FROM @SAM_IG_Questions ) as Ellen
  where RN = @Counter;

我最终使用了datagridview,这似乎也更快了,因为我没有对每个单击事件运行查询

    private int count = 1;
    private void nextQuestion_Click(object sender, EventArgs e)
    {
        //int test = dataGridView1.Rows.Count - 1;
        //MessageBox.Show(dataGridView1.Rows.Count.ToString() + test);

        if (count < (dataGridView1.Rows.Count - 1))
        {
            int question = count + 1;
            questionNumber.Text = "Q" + question;
            iqquestion.Text = dataGridView1.Rows[count].Cells[2].Value.ToString();
            if (dataGridView1.Rows[count].Cells[3].Value != DBNull.Value) { radioButton1.Text = dataGridView1.Rows[count].Cells[3].Value.ToString(); radioButton1.Show(); }
            else { radioButton1.Hide(); }
            if (dataGridView1.Rows[count].Cells[4].Value != DBNull.Value) { radioButton2.Text = dataGridView1.Rows[count].Cells[4].Value.ToString(); radioButton2.Show(); }
            else { radioButton2.Hide(); }
            if (dataGridView1.Rows[count].Cells[5].Value != DBNull.Value) { radioButton3.Text = dataGridView1.Rows[count].Cells[5].Value.ToString(); radioButton3.Show(); }
            else { radioButton3.Hide(); }
            if (dataGridView1.Rows[count].Cells[6].Value != DBNull.Value) { radioButton4.Text = dataGridView1.Rows[count].Cells[6].Value.ToString(); radioButton4.Show(); }
            else { radioButton4.Hide(); }
            if (dataGridView1.Rows[count].Cells[7].Value != DBNull.Value) { radioButton5.Text = dataGridView1.Rows[count].Cells[7].Value.ToString(); radioButton5.Show(); }
            else { radioButton5.Hide(); }
            correctanswer.Text = dataGridView1.Rows[count].Cells[8].Value.ToString();
            instructions.Text = dataGridView1.Rows[count].Cells[9].Value.ToString();
            correctInstructions.Text = instructions.Text;
            count++;
        }
        if (count == (dataGridView1.Rows.Count - 1))
        {
            var homeform = new Home();
            homeform.Show();
            this.Hide();
        }

            panel1.Visible = true;
            iqresult.Visible = false;
    }

如果您使用的是SQL Server,则可以使用ROW_NUMBER对按顺序返回的行进行编号。如果您没有指定ORDERBY子句,那么就没有订单保证。您能给出一个处理数据的示例吗?我如何逐步使用这个我尝试过的问题。Text=Convert.ToStringreader[Description];我试过reader.GetString1;第一次单击下一个问题后,它会显示最后一个问题,然后每次单击都会再次显示最后一个问题。。。我还需要使用计数器并增加GetString@Bowenac-要使这项工作成功,你需要做两件事。首先,将orderbyid添加到查询中,以确保结果以正确的顺序返回。第二,使用循环读取读卡器。读取第一个计数器行。这将使读取器保留在正确的行上,以便您可以访问列值。@Bowenac我已更新了答案。我想你要做的就是下一排谢谢你这看起来很有效。不确定您是否注意到我下面的答案,但我最终使用了datagridview并加载了加载的所有数据。然后通过它访问正确的行。这似乎是一个更好的解决方案,只是好奇而已。因此,我不会在每次单击时都进行查询。看起来很快,只是不确定哪一个是最好的选择。@Bowenac取决于一个用例,但通常最好尽量减少DB往返,除非您处理的是10万个或更多区域中的大量数据,或者您的数据经常更新,并且您总是需要最新的数据。所以DataGridView看起来是一个合理的解决方案。提示一下,如果可以的话,使用列名而不是索引。单元格[ID]而不是单元格[0]我从order by ID上的行\u编号中得到一个调试错误,它不知道这是什么。还有什么是Ellen?这是查询表达式“按ID排序的行数”中缺少运算符的错误语法。@Bowenac-Ellen是TSQL语法所需的占位符。如果你想用罗杰,没关系。我在答案中添加了一个TSQL演示。如果您正在使用Access,所有这些都不起作用。如果您的问题带有数据库和版本标签。