如何使用MysqlDataReader运行多个查询

时间:2017-06-21 22:47:52

标签: c# mysql

我在我的代码中尝试多个查询,如下所示,但它在行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.

2 个答案:

答案 0 :(得分:1)

试试这个:

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

创建第二个connection var

答案 1 :(得分:0)

选项:

  • 在开始内部查询/查询之前,完全使用第一个查询,将数据缓冲为List<T>或类似内容
  • 使用join等一次性读取所有数据,作为单一广泛的结果集 - 并在客户端处理
  • 在单个命令中使用多个select操作来检索多个网格中的数据,并在客户端处理它们
  • 使用多个连接