System.InvalidOperationException: 'There is already an open DataReader associated with this Command which must be closed first.'

System.InvalidOperationException: 'There is already an open DataReader associated with this Command which must be closed first.'

ps : 我试图关闭连接和数据读取器但它没有用,也尝试了这里以前的解决方案但仍然出现错误 我什至分离了连接,但效果不佳 我在这里要做的是从 sql 服务器的 table 中读取一个 id 并使用该 ID 在另一个 table 中查找另一个数据 但它总是给我关闭连接或数据问题

SqlConnection cnx = new SqlConnection("data source = . ; database = tnt ; integrated security = true ; MultipleActiveResultSets=true");
    SqlConnection cnx2 = new SqlConnection("data source = . ; database = tnt ; integrated security = true ; MultipleActiveResultSets=true");



        SqlCommand cmd = new SqlCommand("select matricule from inscription where numformation = @n",cnx);
        cmd.Parameters.AddWithValue("@n", comboBox2.Text);
        SqlDataReader dr = cmd.ExecuteReader();

        List<string> hawhaw = new List<string>();
        while (dr.Read())
        {
            hawhaw.Add(dr[0].ToString());
        }

        cmd.Dispose();

        cnx2.Close();

        cnx2.Open();
        SqlCommand cmd2 = new SqlCommand("select * from person where matricule = @m", cnx2);
        foreach (var item in hawhaw)
        {
            cmd2.Parameters.Clear();
            cmd2.Parameters.AddWithValue("@m", item);
            SqlDataReader dr2 = cmd2.ExecuteReader();

            if (dr2.Read())
            {
                MessageBox.Show(dr2[0].ToString());
            }
        }

        cmd2.Dispose();
        cnx.Close();

最重要的是,您可以管理处置和关闭操作。为此,您应该使用 using 并代表您完成这项工作。

   List<string> hawhaw = new List<string>();

            using (SqlConnection connection = new SqlConnection("data source = . ; database = tnt ; integrated security = true ; MultipleActiveResultSets=true"))
            {
                connection.Open();
                using (SqlCommand cmd = new SqlCommand("select matricule from inscription where numformation = @n", connection))
                {
                    cmd.Parameters.AddWithValue("@n", comboBox2.Text);
                    SqlDataReader dr = cmd.ExecuteReader();

                    while (dr.Read())
                    {
                        hawhaw.Add(dr[0].ToString());
                    }

                }
            }



            using (var connection = new SqlConnection("data source = . ; database = tnt ; integrated security = true ; MultipleActiveResultSets=true"))
            {
                connection.Open();

                foreach (var item in hawhaw)
                {
                    using (var cmd2 = new SqlCommand("select * from person where matricule = @m", connection))
                    {
                        cmd2.Parameters.AddWithValue("@m", item);
                        var dr2 = cmd2.ExecuteReader();
                        if (dr2.Read())
                        {
                            MessageBox.Show(dr2[0].ToString());
                        }
                    }

                }
            }