【问题标题】:How can I find out if I need to call Close() in MySqlConnection when using?使用时如何确定是否需要在 MySqlConnection 中调用 Close()?
【发布时间】:2018-10-14 08:56:18
【问题描述】:

使用 MySql.Data.MySqlClient;

带有“使用”的简单连接代码:

using (IDbConnection sql = new MySqlConnection(ConnectionString))
     {
           try
           {
                sql.Open();
                var x = sql.Execute("query...");
           }
            catch (Exception ex)
           {
                 Console.WriteLine(ex.Message);
           }
     }

“Using”调用Dispose(),但如果Dispose()调用Close()?

在MySqlConnection中使用时如何判断是否需要调用Close()?

【问题讨论】:

    标签: c# dispose using mysql-connector mysqlconnection


    【解决方案1】:

    什么都不做。

    Dispose 将正确关闭连接。

    来自文档:

    Dispose() - 释放 MySqlConnection 使用的所有资源

    形成MySqlConnection.Dispose的源码

    void IDisposable.Dispose()
    {
        if (State == ConnectionState.Open)
            Close();
    }
    

    【讨论】:

      【解决方案2】:

      这里你可以试试,你可以把它放在finally子句中

      if (sql.State == ConnectionState.Open)
      {
          sql.Close();
      }
      

      【讨论】:

        【解决方案3】:

        这是我正在使用的示例 DBConnect 类。有了这个类,在任何你想要的地方创建一个数据库连接真的很容易。只需创建此类的一个对象。

        这是 MySQL 数据库,如果您使用的是 sql server,只需将 MySql 替换为 Sql

        这是我的 DBConnect 课程

        using System    ;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
        using MySql.Data.MySqlClient;
        
        
        namespace test
        {
        
            class DBConnect: IDisposable 
            {
                private static String server = "localhost";
                private static String port = "3306";
                private static String database = "testDB";
                private static String username = "root";
                private static String password = "";
        
                private static String connectionString = "Server=" + server + ";Port=" + port + ";Database=" + database + ";Uid=" + username + ";Password=" + password + ";";
                 public MySqlConnection con = new MySqlConnection(connectionString);
        
        
        
                public DBConnect() //Constructor
                {
        
                    try
                    {
                        con.Open();
                        Console.WriteLine("Database connected");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.StackTrace);
                        Console.WriteLine("Database Connection Failed");
                        throw new Exception();
                    }
        
        
                }
        
        
                public void Dispose()
                {
                    con.Close();
                }
            }
        }
        

        这是你如何使用这个类

        using(DBConnect db = new DBConnect())
        {
            String q = "your sql Statement here";
            MySqlCommand cmd = new MySqlCommand(q, db.con);
            cmd.ExecuteNonQuery();
            MessageBox.Show("Item added", "Done", MessageBoxButtons.OK,mesageBoxIcon.Information);
                        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-10-14
          • 2011-08-04
          • 2012-08-03
          • 2017-06-06
          • 2011-09-23
          • 2012-08-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多