c# – 如何使用MysqlDataReader运行多个查询

我正在尝试在我的代码中进行多个查询,如下所示,但它会在
MySqlDataReader行抛出异常rdr_get_latest_build_notes = get_latest_build_notes_cmd.ExecuteReader();因为我已经开了一个rdr

我用Google搜索并发现@ according to MSDN不受支持,我需要有关如何修复它的指导?

    public IEnumerable<BuildNotes_op> RetrieveBuildDetails(string BuildID)
    {
        ....
        var conn = new MySql.Data.MySqlClient.MySqlConnection();
        conn.ConnectionString = AciDev.aciDevConnectionString;
        try
        {
            Console.WriteLine("Connecting to MySQL...");
            conn.Open();
            string sql = @"query1";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            MySqlDataReader rdr = cmd.ExecuteReader(); --> **rdr open here**
            ....

            while (rdr.Read())
            {
                .....
                if (rdr[4] != DBNull.Value)
                    ....................
                else
                     ........
                     string sql_get_latest_build_notes_cmd = "query2";

                     MySqlCommand get_latest_build_notes_cmd = new MySqlCommand(sql_get_latest_build_notes_cmd, conn);
                     MySqlDataReader rdr_get_latest_build_notes = get_latest_build_notes_cmd.ExecuteReader();  --> **throws exception here**
                     if (rdr_get_latest_build_notes.HasRows)
                    {
                      ....................

                    }
                     rdr_get_latest_build_notes.Close();

                if (BuildDetails != null)
                {
                    BuildNotesDetails.Add(BuildDetails);
                }
            }
            rdr.Close();
            conn.Close();
            return BuildNotesDetails;
        }

        catch
        {
            throw;
        }
        finally
        {
            conn.Close();
        }

例外:

There is already an open DataReader associated with this Connection which must be closed first.

最佳答案 试试这个:

  public IEnumerable<BuildNotes_op> RetrieveBuildDetails(string BuildID)
    {

        var conn = new MySql.Data.MySqlClient.MySqlConnection();
         var conn2 = new MySql.Data.MySqlClient.MySqlConnection();
        conn.ConnectionString = AciDev.aciDevConnectionString;
        try
        {
            Console.WriteLine("Connecting to MySQL...");
            conn.Open();
            string sql = @"query1";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            MySqlDataReader rdr = cmd.ExecuteReader(); --> **rdr open here**
            ....

            while (rdr.Read())
            {
                .....
                if (rdr[4] != DBNull.Value)
                    ....................
                else
                     ........
                     string sql_get_latest_build_notes_cmd = "query2";

                     conn2.ConnectionString = AciDev.aciDevConnectionString;

                 Console.WriteLine("Connecting to MySQL...");
                  conn2.Open();
                     MySqlCommand get_latest_build_notes_cmd = new MySqlCommand(sql_get_latest_build_notes_cmd, conn2);
                     MySqlDataReader rdr_get_latest_build_notes = get_latest_build_notes_cmd.ExecuteReader();  --> **throws exception here**
                     if (rdr_get_latest_build_notes.HasRows)
                    {
                      ....................

                    }
                     rdr_get_latest_build_notes.Close();
                    conn2.Close();

                if (BuildDetails != null)
                {
                    BuildNotesDetails.Add(BuildDetails);
                }
            }
            rdr_get_latest_build_notes.Close();
            rdr.Close();
            conn.Close();
            conn2.Close();
            return BuildNotesDetails;
        }

        catch
        {
            throw;
        }
        finally
        {
            rdr_get_latest_build_notes.Close();
            rdr.Close();
            conn.Close();
            conn2.Close();
        }

创建第二个连接var

点赞