C# 每次单击按钮时获取access数据库表的下一行
我试图从access数据库的表中获取下一行,但一直都是最后一行 请指导我如何在所有行中循环 代码如下:C# 每次单击按钮时获取access数据库表的下一行,c#,ms-access-2007,oledb,C#,Ms Access 2007,Oledb,我试图从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(
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();
}
protectedvoidbtn\u clk(对象发送方,事件参数e)
{
string constr=@“Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:\Users\Documents\databaseb.mdb”;
string cmdstr1=“从表中选择计数(*)”;
OLEDB连接con1=新OLEDB连接(CONT);
OleDbCommand com1=新的OleDbCommand(cmdstr1,con1);
con1.Open();
int count=(int)com1.ExecuteScalar();
int i=2;
而(i=必须在按钮外单击:
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++;
}
如果(i听起来你在追求
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的第一条记录。例如,如果第一条记录是ID0
,则它将找到的下一条记录是1
,依此类推
当然,这只有在您有顺序ID的情况下才有效。假设您的表不大(意味着它包含许多记录),您可以尝试在表单的开头加载所有内容,并将这些数据存储在datatable中。
此时,按钮中的逻辑应该只是将指针向前移动到要显示的记录
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中数据绑定问题的一种拙劣方法,因此您可能应该看看
希望您的表不是真正命名为table
,该单词是Access的保留关键字只需删除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();
}
Whenwhile(i此表中有多少行?为什么要遍历所有记录并每次覆盖所有内容?是的,这将只显示最后一行,因为在检索它之后,您已经覆盖了所有其他内容。您打算如何显示多行,以及如何确定哪些行是您想要的行?它是n从这个问题上看不清楚。在这里使用分页而不是拉取所有内容将是一个更好的解决方案。我在答案的开头已经说过。我们不知道分页所涉及的复杂性是否是OP问题的真正好处。