0

我正在尝试从访问数据库表中获取下一行,但我一直在获取最后一行。

请指导我如何遍历所有行?

这是代码:

  protected void btn_clk(object sender, EventArgs e)
  {
      string constr = @"Provider=Microsoft.Jet.OLEDB.4.0; DataSource=C:\Users\Documents\databaseb.mdb";
      string cmdstr1 = "select count(*) from table";

      OleDbConnection con1 = new OleDbConnection(constr);
      OleDbCommand com1 = new OleDbCommand(cmdstr1, con1);

      con1.Open();

      int count = (int) com1.ExecuteScalar();
      int i = 2;
      while(i<=count)
      {
          string cmdstr = "select * from table where id = " + i;

          OleDbConnection con = new OleDbConnection(constr);
          OleDbCommand com = new OleDbCommand(cmdstr, con);

          con.Open();

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

          label1.Text = String.Format("{0}", reader[1]);
          RadioButton1.Text = String.Format("{0}", reader[2]);
          RadioButton2.Text = String.Format("{0}", reader[3]);
          RadioButton3.Text = String.Format("{0}", reader[4]);
          RadioButton4.Text = String.Format("{0}", reader[5]);

          con.Close();
          i++;
      }

      con1.Close();
  }
4

4 回答 4

0

=It`s must outside a button click:

string constr = @"Provider=Microsoft.Jet.OLEDB.4.0; Data     Source=C:\Users\Documents\databaseb.mdb";
     string cmdstr1 = "select count(*) from table";
     OleDbConnection con1 = new OleDbConnection(constr);
     OleDbCommand com1 = new OleDbCommand(cmdstr1, con1);
     con1.Open();
     int count = (int) com1.ExecuteScalar();
     int i = 2;

     con1.Close();

Its must inside button click. You must make i property of form

    if(i<=count)
{
     string cmdstr = "select * from table where id = " + i;
       OleDbConnection con = new OleDbConnection(constr);
       OleDbCommand com = new OleDbCommand(cmdstr, con);
       con.Open();
       OleDbDataReader reader = com.ExecuteReader();
       reader.Read();
       label1.Text = String.Format("{0}", reader[1]);
       RadioButton1.Text = String.Format("{0}", reader[2]);
       RadioButton2.Text = String.Format("{0}", reader[3]);
       RadioButton3.Text = String.Format("{0}", reader[4]);
       RadioButton4.Text = String.Format("{0}", reader[5]);
       con.Close();
       i++;     
}

But i think it`s may rewrite to more beauty solve.

于 2013-10-18T08:40:59.950 回答
0

听起来你在追求类似的东西

private int _currentRecordId = -1;
...

string cmdStr = String.Format("SELECT * FROM Table WHERE id > {0} ORDER BY id LIMIT 1", _currentRecordId);
using (var con = new OleDbConnection(constr))
using (var com = new OleDbCommand(cmdStr, con))
{
    con.Open();
    using(var reader = com.ExecuteReader())
    {
        while (reader.Read())
        {
            _currentRecordId = reader.GetInt32(0); // whatever field the id column is
            // populate fields
        }
    }
}

在第一次调用时,这将检索具有 ID > 的第一条记录-1。然后它记录此记录的任何 ID,因此在下一次调用时,它将采用大于该记录的第一条记录,例如,如果第一条记录是 id 0,那么它将找到的下一条记录是1等等......

当然,这只有在您有顺序 ID 时才真正有效。

于 2013-10-18T08:45:28.533 回答
0

只需删除 for 循环即可一一获取记录

int lastFetchedRecordIndex=2;
protected void btn_clk(object sender, EventArgs e)
{
     string constr = @"Provider=Microsoft.Jet.OLEDB.4.0; Data     Source=C:\Users\Documents\databaseb.mdb";
     string cmdstr1 = "select count(*) from table";
     OleDbConnection con1 = new OleDbConnection(constr);
     OleDbCommand com1 = new OleDbCommand(cmdstr1, con1);
     con1.Open();
     int count = (int) com1.ExecuteScalar();
       string cmdstr = "select * from table where id = " + lastFetchedRecordIndex;
       OleDbConnection con = new OleDbConnection(constr);
       OleDbCommand com = new OleDbCommand(cmdstr, con);
       con.Open();
       OleDbDataReader reader = com.ExecuteReader();
       reader.Read();
       label1.Text = String.Format("{0}", reader[1]);
       RadioButton1.Text = String.Format("{0}", reader[2]);
       RadioButton2.Text = String.Format("{0}", reader[3]);
       RadioButton3.Text = String.Format("{0}", reader[4]);
       RadioButton4.Text = String.Format("{0}", reader[5]);
       con.Close();
       lastFetchedRecordIndex++;
     con1.Close();
  }
于 2013-10-18T09:46:56.197 回答
0

假设您的表不大(意味着它包含可管理数量的记录),您可以尝试在表单打开时加载所有内容并将该数据存储在数据表中。
此时,按钮中的逻辑应该只是将指针前进到要显示的记录。

private DataTable data = null;
private int currentRecord = 0;
protected void form_load(object sender, EventArgs e)
{
     using(OleDbConnection con1 = new OleDbConnection(constr))
     using(OleDbCommand cmd = new OleDbCommand("select * from table", con1))
     {
         con1.Open();
         using(OleDbDataAdapter da = new OleDbDataAdapter(cmd))
         {
             data = new DataTable();
             da.Fill(data);
         }
         DisplayCurrentRecord();
    }
}
private void DisplayCurrentRecord()
{
    label1.Text = String.Format("{0}", data.Rows[currentRecord][1]);
    RadioButton1.Text = String.Format("{0}", data.Rows[currentRecord][2]);
    RadioButton2.Text = String.Format("{0}", data.Rows[currentRecord][3]);
    RadioButton3.Text = String.Format("{0}", data.Rows[currentRecord][4]);
    RadioButton4.Text = String.Format("{0}", data.Rows[currentRecord][5]);
}
protected void btn_clk(object sender, EventArgs e)
{
    if(currentRecord >= data.Rows.Count - 1)
        currentRecord = 0;
    else
        currentRecord++;
    DisplayCurrentRecord();
}

请记住,这只是一个示例。应该对行(如果它们包含空值)以及返回的表中是否有行进行稳健检查。此外,对于 WinForm 中的 DataBindings 问题,这是一个穷人的方法,所以也许你应该看一些关于这个主题的教程

希望您的表没有真正命名Table,该词是 Access 的保留关键字

于 2013-10-18T08:50:45.477 回答