在不知道任何记录的情况下,对每一行单击遍历表

本文关键字:一行 单击 遍历 记录 任何 不知道 情况下 | 更新日期: 2023-09-27 18:12:04

每次单击按钮时,我都试图循环通过表。目前,我已经将其设置为增加计数器,然后仅获取计数器编号的id匹配。但这不是一个好方法,因为我发现表中的ID列不一致。在ID 2之后,它跳到5,所以它是1-2-3 -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;
    }

在不知道任何记录的情况下,对每一行单击遍历表

WHERE条款去掉

cmdGet.CommandText = "SELECT * FROM SAM_IG_Questions";

然后用

遍历行
while(reader.Read())
{
    //handle row data  
}

编辑:示例如何使用OleDbDataReader。阅读器方法:http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbdatareader.read(v=vs.110).aspx

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 *, Row_Number() over ( order by Id ) as RN FROM SAM_IG_Questions ) as Ellen where RN = @counter";

这将生成一列序列数字(RN),其顺序与Id列值相同,然后只选择行号与计数器匹配的行。

在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;
    }
相关文章: