【发布时间】: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<=count)工作结束时,您在标签的最后一行。 -
这张表有多少行?
-
为什么每次都遍历所有记录并覆盖所有内容?是的,这只会显示最后一行,因为您在检索后覆盖了其他所有内容。您打算如何显示多行以及您打算如何识别哪些行是您想要的?从问题上看不清楚。
标签: c# ms-access-2007 oledb