错误:已经有一个与此命令关联的打开DataReader,必须先关闭它

时间:2016-06-08 12:08:11

标签: c# asp.net asp.net-mvc web webforms

我在同一个打开的Connection上设置很多命令时遇到问题,它总是说有一个与命令关联的DataReader必须关闭,虽然我关闭了它仍然给我同样的问题,这是我的代码:

 protected void Afficher_click(object sender, EventArgs e)
    {
        using (SqlConnection con = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand("select * from Consommation where idAbonnement = @ab and periode between @d1 and @d2 ",con);
            cmd.Parameters.AddWithValue("@ab", DropDownList1.SelectedValue);
            cmd.Parameters.AddWithValue("d1", TextBox1.Text);
            cmd.Parameters.AddWithValue("d2", TextBox2.Text);
            con.Open();
            GridView1.DataSource = cmd.ExecuteReader();
            GridView1.DataBind();
            //cmd.ExecuteReader().Close();
            cmd = new SqlCommand("select SUM(Qte) from Consommation where idAbonnement =" + DropDownList1.SelectedValue, con);
            Label2.Text = cmd.ExecuteScalar().ToString();
            cmd = new SqlCommand("select AVG(Qte) from Consommation where idAbonnement =" + DropDownList1.SelectedValue, con);
            Label4.Text = cmd.ExecuteScalar().ToString();
        }

您能否向我解释一下究竟是什么问题以及如何解决? 先谢谢。

5 个答案:

答案 0 :(得分:1)

您应该关闭SqlDataReader。在这种情况下,最好的方法是使用:

GridView1.DataSource = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

或使用其他方法,例如使用SqlDataAdapterDataTable

string sql = "select * from Consommation " +
             "where idAbonnement = @ab " +
             "and periode between @d1 and @d2 ";
using (SqlConnection con = new SqlConnection(cs))
using (SqlDataAdapter da = new SqlDataAdapter(sql, con))
{
    con.Open();
    var cmd = da.SelectCommand;
    cmd.Parameters.AddWithValue("@ab", DropDownList1.SelectedValue);
    cmd.Parameters.AddWithValue("d1", TextBox1.Text);
    cmd.Parameters.AddWithValue("d2", TextBox2.Text);
    DataTable dataSource = new DataTable();
    da.Fill(dataSource);
    GridView1.DataSource = dataSource;
    GridView1.DataBind();

   using (var cmdSumQte = new SqlCommand(
        "select SUM(Qte) from Consommation where idAbonnement = @idAbonnement", con))
    {
        cmdSumQte.Parameters.Add("@idAbonnement", SqlDbType.Int).Value =
            int.Parse(DropDownList1.SelectedValue);
        Label2.Text = cmdSumQte.ExecuteScalar().ToString();
    }

    using(var cmdAvgQte = new SqlCommand(
            "select AVG(Qte) from Consommation where idAbonnement = @idAbonnement", con))
    {
        cmdAvgQte.Parameters.Add("@idAbonnement", SqlDbType.Int).Value =
            int.Parse(DropDownList1.SelectedValue);
        Label4.Text = cmdAvgQte.ExecuteScalar().ToString();
    }
}

请注意,我还使用了其他命令的参数,也使用了多个。在这种情况下,你可以避免粗心的错误。

答案 1 :(得分:0)

尝试为第二个命令使用不同的名称:

 using (SqlConnection con = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand("select * from Consommation where idAbonnement = @ab and periode between @d1 and @d2 ",con);
            cmd.Parameters.AddWithValue("@ab", DropDownList1.SelectedValue);
            cmd.Parameters.AddWithValue("d1", TextBox1.Text);
            cmd.Parameters.AddWithValue("d2", TextBox2.Text);
            con.Open();
            GridView1.DataSource = cmd.ExecuteReader();
            GridView1.DataBind();
            cmd.Dispose();
            SqlCommand cmd2 = new SqlCommand("select SUM(Qte) from Consommation where idAbonnement =" + DropDownList1.SelectedValue, con);
            Label2.Text = cmd2.ExecuteScalar().ToString();
            SqlCommand cmd3 = new SqlCommand("select AVG(Qte) from Consommation where idAbonnement =" + DropDownList1.SelectedValue, con);
            Label4.Text = cmd3.ExecuteScalar().ToString();
        }

此外,非常重要:使用参数,您接触SQL注入

答案 2 :(得分:0)

protected void Afficher_click(object sender, EventArgs e)
    {
        using (SqlConnection con = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand("select * from Consommation where idAbonnement = @ab and periode between @d1 and @d2 ",con);
            cmd.Parameters.AddWithValue("@ab", DropDownList1.SelectedValue);
            cmd.Parameters.AddWithValue("d1", TextBox1.Text);
            cmd.Parameters.AddWithValue("d2", TextBox2.Text);
            con.Open();
            GridView1.DataSource = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
            GridView1.DataBind();
            cmd.Dispose();
            cmd = null;
            cmd = new SqlCommand("select SUM(Qte) from Consommation where idAbonnement =" + DropDownList1.SelectedValue, con);
            Label2.Text = cmd.ExecuteScalar().ToString();
            cmd = new SqlCommand("select AVG(Qte) from Consommation where idAbonnement =" + DropDownList1.SelectedValue, con);
            Label4.Text = cmd.ExecuteScalar().ToString();
        }

答案 3 :(得分:0)

由于您使用的是多个结果集,因此需要添加以下语句

MultipleActiveResultSets = TRUE;

在你的连接字符串中。

例如,

服务器= myServerAddress;数据库= MYDATABASE; Trusted_Connection = TRUE; MultipleActiveResultSets = TRUE;

答案 4 :(得分:0)

这是我尝试过的解决方案,根据此处的帖子工作完美:https://blogs.msdn.microsoft.com/spike/2009/08/20/there-is-already-an-open-datareader-associated-with-this-command-which-must-be-closed-first-explained/

            using (SqlDataReader rd = cmd.ExecuteReader())
            {
                GridView1.DataSource = rd;
                GridView1.DataBind();
            }