错误已经存在与此连接关联的打开的datareader,必须先关闭该连接 [英] Error there is already an open datareader associated with this connection which must be closed first

查看:122
本文介绍了错误已经存在与此连接关联的打开的datareader,必须先关闭该连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!我一直得到同样的错误:已经有一个与此Connection关联的开放DataReader必须先关闭。



如何摆脱它?这导致我的程序挂起。



Hi! I keep getting the same error: There is already an open DataReader associated with this Connection which must be closed first.

How do I get rid of it? It's causing my program to hang.

public static void GetEntryStatus()
        {
            try
            {
                OpenConnection();
                MySqlCommand cmd = new MySqlCommand();
                cmd.CommandText = "SELECT tbl_param.logid, COUNT( tbl_parkinfo.parkid) " +
                                "FROM tbl_param " +
                                "INNER JOIN tbl_parkinfo ON tbl_param.logid = tbl_parkinfo.entrylogid " +
                                "WHERE tbl_param.area_id = @area_id " +
                                "AND (tbl_parkinfo.entrydate >= @startdate AND tbl_parkinfo.entrydate <= @enddate) " +
                                "GROUP BY tbl_param.logid ";
                cmd.Parameters.AddWithValue("@area_id", GlobalVariables.AreaID);
                cmd.Parameters.AddWithValue("@startdate", GlobalVariables.SStartDate);
                cmd.Parameters.AddWithValue("@enddate", GlobalVariables.SEndDate);
                cmd.Connection = connection;
                MySqlDataReader x = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

                TerminalNames = "";
                TerminalNumbers = "";
                TotalEntry = 0;

                while (x.Read())
                {
                    TerminalNames += x.IsDBNull((0)) ? null : x.GetString(0) + Environment.NewLine;
                    TerminalNumbers += x.IsDBNull((1)) ? null : x.GetString(1) + Environment.NewLine;
                    TotalEntry += x.IsDBNull((1)) ? 0 : x.GetInt32(1);
                }
                CloseConnection();
            }
            catch (MySqlException ex) { GlobalFunctions.addMsgToLog("GetEntryStatus: " + ex.Message); }
            finally { CloseConnection(); }
        }





我尝试过:



试用1

试用版2

推荐答案

你的 OpenConnection()方法很漂亮因为它显然打开了一个连接,可以在全班同学中广泛使用,因此无法使用。这是不好的做法。如果你需要两个连接怎么办?您的代码不支持它。



正确的方法是尽可能晚地打开与数据库的连接,使数据库的工作效率与可能,并尽早关闭连接。哦,并处理它。



一个更好的方法,而不是 OpenConnection()将是实际返回连接对象的GetConnection()。这使您可以控制连接对象的Dipose,而不是在垃圾收集到达时依赖于它。
Your OpenConnection() method is pretty much usless as it's apparently opening a single connection for use widely across your class. This is BAD PRACTICE. What if you needed two connections? Your code will not support it.

The proper way to do this would be to open your connection to the database as late as possible, do your database work as efficiently as possible, and close the connection as early as possible. Oh, and Dispose of it.

A better method, instead of OpenConnection() would be GetConnection() that actually returns a connection object. This lets you control the Dipose of the connection object instead of you relying on whenever the garbage collection gets around to it.


这篇关于错误已经存在与此连接关联的打开的datareader,必须先关闭该连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆