从2位读者那里获取值时出错。
以下是我的代码。
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con;
con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\ASPNETDB.MDF;Integrated Security=True;User Instance=True");
SqlCommand com = new SqlCommand();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
string result = "SELECT AVG(p_tan) AS p_tang, AVG(e_tan) AS e_tang, AVG(p_rel) AS p_reli, AVG(e_rel) AS e_reli, AVG(p_res) AS p_resp, AVG(e_res) AS e_resp, AVG(p_assu) AS p_assur, AVG(e_assu) AS e_assur, AVG(p_emp) AS p_emph, AVG(e_emp) AS e_emph FROM stats";
SqlCommand showresult = new SqlCommand(result, con);
con.Open();
SqlDataReader reader = showresult.ExecuteReader();
while (reader.Read())
{
Label1.Text = reader["p_tang"].ToString().Trim();
Label2.Text = reader["e_tang"].ToString().Trim();
Label3.Text = reader["p_reli"].ToString().Trim();
Label4.Text = reader["e_reli"].ToString().Trim();
Label5.Text = reader["p_resp"].ToString().Trim();
Label6.Text = reader["e_resp"].ToString().Trim();
Label7.Text = reader["p_assur"].ToString().Trim();
Label8.Text = reader["e_assur"].ToString().Trim();
Label9.Text = reader["p_emph"].ToString().Trim();
Label10.Text = reader["e_emph"].ToString().Trim();
reader.Close();
reader.Dispose();
string re = "SELECT AVG(p_tan) AS p_tang1, AVG(e_tan) AS e_tang1, AVG(p_rel) AS p_reli1, AVG(e_rel) AS e_reli1, AVG(p_res) AS p_resp1, AVG(e_res) AS e_resp1, AVG(p_assu) AS p_assur1, AVG(e_assu) AS e_assur1, AVG(p_emp) AS p_emph1, AVG(e_emp) AS e_emph1 FROM stats_other";
SqlCommand manager = new SqlCommand(re, con);
SqlDataReader rea = showresult.ExecuteReader();
while (rea.Read())
{
Label33.Text = rea["p_tang1"].ToString().Trim();
Label34.Text = rea["e_tang1"].ToString().Trim();
Label23.Text = rea["p_reli1"].ToString().Trim();
Label24.Text = rea["e_reli1"].ToString().Trim();
Label41.Text = rea["p_resp1"].ToString().Trim();
Label42.Text = rea["e_resp1"].ToString().Trim();
Label28.Text = rea["p_assur1"].ToString().Trim();
Label29.Text = rea["e_assur1"].ToString().Trim();
Label37.Text = rea["p_emph1"].ToString().Trim();
Label38.Text = rea["e_emph1"].ToString().Trim();
当我运行它时会发出错误消息“索引超出范围异常未被用户代码处理”
任何帮助都是适当的
答案 0 :(得分:4)
string re = "SELECT AVG(p_tan) AS p_tang1, AVG(e_tan) AS e_tang1, AVG(p_rel) AS p_reli1, AVG(e_rel) AS e_reli1, AVG(p_res) AS p_resp1, AVG(e_res) AS e_resp1, AVG(p_assu) AS p_assur1, AVG(e_assu) AS e_assur1, AVG(p_emp) AS p_emph1, AVG(e_emp) AS e_emph1 FROM stats_other";
SqlCommand manager = new SqlCommand(re, con);
SqlDataReader rea = showresult.ExecuteReader();
您刚刚为原始命令创建了第二个阅读器
你永远不会使用manager
。