每次单击按钮获取访问数据库的下一行表

时间:2013-10-18 08:27:10

标签: c# ms-access-2007 oledb

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

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

以下是代码:

  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 个答案:

答案 0 :(得分:0)

=它必须在按钮点击之外:

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();

必须在按钮内单击。您必须提供表单<{p>>的i属性

    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++;     
}

但我认为这可能会改写为更多美容解决方案。

答案 1 :(得分: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为&gt;的第一条记录。 -1。然后它记录该记录的ID是什么,然后在下一次呼叫时,它将使第一个记录大于该记录。如果第一条记录为ID 0,那么它将找到的下一条记录为1,依此类推......

如果你当然有顺序ID,这才真正有用。

答案 2 :(得分: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问题,这是一个穷人的方法,所以也许你应该看看some tutorials on this subject

希望您的表名不是Table,该字是Access的保留关键字

答案 3 :(得分: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();
  }