C#MySqlConnection不会关闭 [英] C# MySqlConnection won't close

查看:461
本文介绍了C#MySqlConnection不会关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,触发一个mysql命令(查询)显示数据库,查询工作和正确返回,但我不能关闭我的连接。我使用的用户有24个连接允许同时,所以问题弹出进一步下来我的程序,但减少允许连接到2显示我,我甚至不能关闭第一个查询(这不是一个循环)。代码如下:

I have an application that fires a mysql command (query) "show databases", the query works and returns properly but I can't close my connections. The user I used had 24 connections allowed at the same time so the problem popped up further down my program but reducing the allowed connections to 2 shows me that I can't even close the first query (which isn't in a loop). The code is the following:

    protected override Dictionary<string, Jerow_class_generator.Database> loadDatabases()
    {
        MySqlConnection sqlCon = new MySqlConnection(this.ConnectionString);
        sqlCon.Open();

        MySqlCommand sqlCom = new MySqlCommand();
        sqlCom.Connection = sqlCon;
        sqlCom.CommandType = CommandType.Text;
        sqlCom.CommandText = "show databases;";

        MySqlDataReader sqlDR;
        sqlDR = sqlCom.ExecuteReader();

        Dictionary<string, Jerow_class_generator.Database> databases = new Dictionary<string, Jerow_class_generator.Database>();
        string[] systemDatabases = new string[] { "information_schema", "mysql" };

        while (sqlDR.Read())
        {
            string dbName = sqlDR.GetString(0);
            if (!systemDatabases.Contains(dbName))
            {
                databases.Add(sqlDR.GetString(0), new MySQL.Database(dbName, this));
            }
        }

        sqlCom.Dispose();
        sqlDR.Close();

        sqlCon.Close();
        sqlCon.Dispose();
        return databases;
    }

'new MySQL.Database(dbName,this));'是我的owm做的类,只存储数据库结构,可以认为是不相关的。

P.S. The 'New MySQL.Database(dbName, this));' is my owm made class which only stores the DB structure, could be considered irrelevant.

get是'max_user_connections'。

The exact error I get is 'max_user_connections'. on the connection.open line of the next time a query needs to be fired.

推荐答案

在下一次查询需要被触发的connection.open行上。 code>打开 / 关闭 / 在所有地方调用建议只使用 使用语句替换所有这些。这将确保每个对象的预期范围是清楚的,并且在退出该范围时将被销毁/处置。

Rather than keeping track of all the Open/Close/Dispose calls all over the place, I'd recommend just replacing all of those with using statements. This will make sure the expected scope of each object is clear and that it will be destroyed/disposed upon exiting that scope.

这篇关于C#MySqlConnection不会关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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