Jeff Short
Jeff Short

Reputation: 285

How to run multiple queries using MysqlDataReader

I am trying to multiple queries in my code as below but it throws exception at line MySqlDataReader rdr_get_latest_build_notes = get_latest_build_notes_cmd.ExecuteReader(); since I already have an rdr open

I googled and found @according to MSDN its not supported,I need guidance on how to fix it?

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

Exception:

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

Upvotes: 4

Views: 1371

Answers (2)

Vijunav Vastivch
Vijunav Vastivch

Reputation: 4191

try this:

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

Creating a second connection var

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1062580

options:

  • consume the first query completely, buffering the data into a List<T> or similar, before starting the inner query / queries
  • use join etc to read all the data in one go, as a single - wide -result set - and process it at the client
  • use multiple select operations in a single command to retrieve the data in multiple grids, and process them at the client
  • use multiple connections

Upvotes: 0

Related Questions