0

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

当我运行它给出一个错误说“用户代码未处理索引超出范围异常”

任何帮助将不胜感激

4

1 回答 1

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.

于 2013-07-14T19:33:30.713 回答