【问题标题】:Getting next row of table of access database every time on clicking a button每次单击按钮时获取访问数据库表的下一行
【发布时间】:2013-10-18 08:27:09
【问题描述】:

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

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

代码如下:

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

【问题讨论】:

  • while(i&lt;=count) 工作结束时,您在标签的最后一行。
  • 这张表有多少行?
  • 为什么每次都遍历所有记录并覆盖所有内容?是的,这只会显示最后一行,因为您在检索后覆盖了其他所有内容。您打算如何显示多行以及您打算如何识别哪些行是您想要的?从问题上看不清楚。

标签: c# ms-access-2007 oledb


【解决方案1】:

=必须在按钮点击之外:

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

它必须在按钮内点击。您必须将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++;     
}

但我认为它可能会重写以解决更多美容问题。

【讨论】:

    【解决方案2】:

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

    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 时才真正有效。

    【讨论】:

      【解决方案3】:

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

      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 的保留关键字

      【讨论】:

      • 在这里使用分页而不是拉取所有内容会是一个更好的解决方案。
      • 我在答案的开头已经说过了。我们不知道分页所涉及的复杂性是否是 OP 问题的真正收益。
      【解决方案4】:

      只需去掉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();
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-06
        • 1970-01-01
        • 2011-02-24
        • 1970-01-01
        • 1970-01-01
        • 2015-11-04
        • 2022-08-08
        • 1970-01-01
        相关资源
        最近更新 更多