微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐