从 VS C# 2010 中的访问数据库中获取下一行
本文关键字:一行 获取 数据库 VS 2010 访问 | 更新日期: 2023-09-27 18:35:49
我已经尝试了以下代码,但它抛出异常"没有为一个或多个必需参数提供值"。
protected void Button2_Click(object sender, EventArgs e)
{
string constr = @"Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=C:'Users'yogi'Documents'mydb.mdb";
string cmdstr1 = "select count(*) from quant_level1";
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 quant_level1 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();
}
一个错误在 select 语句中:您正在尝试选择未提供的 ID。
string cmdstr = "select * from quant_level1 where id = i";
您的选择语句应如下所示:
string cmdstr = "select * from quant_level1 where id = " + i;
您的循环计数器在您的字符串中。它不会自动替换。
试试这个 -
仅在使用 Web 应用程序时工作
protected void Button2_Click(object sender, EventArgs e)
{
int i;
if (ViewState["count"] != null)
i = Convert.ToInt32(ViewState["count"].ToString());
else
i = 2; // assuming that it is your starting point as given in question
string constr = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:'Users'yogi'Documents'mydb.mdb";
string cmdstr1 = "select count(*) from quant_level1";
OleDbConnection con1 = new OleDbConnection(constr);
OleDbCommand com1 = new OleDbCommand(cmdstr1, con1);
con1.Open();
int count = (int)com1.ExecuteScalar();
if (i < count)
{
string cmdstr = "select * from quant_level1 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++;
ViewState["count"] = i.ToString();
}
con1.Close();
}
这将在您的情况下解决问题。 快乐编码:)
因为每次单击按钮都想获取下一行。
int i=2;
protected void Button2_Click(object sender, EventArgs e)
{
string constr = @"Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=C:'Users'yogi'Documents'mydb.mdb";
string cmdstr1 = "select * from quant_level1 where id ="+i;
OleDbConnection con1 = new OleDbConnection(constr);
OleDbCommand com1 = new OleDbCommand(cmdstr1, con1);
con1.Open();
if(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]);
i++;
}
con1.Close();
}