我自己写了一段数据库连接的代码,在析构函数里边调用了ADODB.Connection的close方法,不过不知道为什么没有关闭掉数据库连接啊。    class GF_ADODB
    {
        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        private string sqlstr;        /// <summary>
        /// 记录数据库连接的ADODB.Connection对象的实例
        /// </summary>
        private Connection conn = new Connection();         /// <summary>
        /// 用于容纳一个来自数据库表的的ADODB.Connection对象的实例
        /// </summary>
        private ADODB.Recordset AdoRs = new Recordset();        /// <summary>
        /// 初始化数据连接类GF_ADODB
        /// </summary>
        /// <param name="_Sqlstr">数据库连接字符串</param>
        public GF_ADODB(string _Sqlstr)
        {
            this.sqlstr = _Sqlstr;
        }        /// <summary>
        /// 析构函数
        /// </summary>
        ~GF_ADODB()
        {
            try
            {
                MessageBox.Show("析构");
                if (AdoRs == null)
                {
                    AdoRs.Close();
                    AdoRs = null;
                }
                if (conn == null)
                {
                    conn.Close();
                }           
            }
            catch { }
        }        /// <summary>
        /// 获取或设置数据库连接字符串
        /// </summary>
        public string  Sqlstr
        {
            set { sqlstr = value; }
            get { return sqlstr; }
        }        /// <summary>
        /// 获取记录数据库连接的ADODB.Connection对象的实例
        /// </summary>
        public Connection Conn
        {
            get { return conn; }
        }        /// <summary>
        /// 获取一个ADODB数据库连接
        /// </summary>
        /// <returns>记录数据库连接的ADODB.Connection对象的实例</returns>
        public Connection Gf_conn()
        {
            if (_Conn())
            {
                return conn;
            }
            else
            {
                return null;
            }
        }        /// <summary>
        /// 建立ADODB数据库连接
        /// </summary>
        /// <param name="conn">记录数据库连接的ADODB.Connection对象的实例</param>
        /// <returns>返回一个布尔类型,成功建立数据库连接为true,否则为false</returns>
        private Boolean _Conn()
        {
            try
            {                   
                conn.CommandTimeout = 10;  
                conn.ConnectionTimeout = 1;
                conn.ConnectionString = sqlstr;
                conn.Open(sqlstr, "", "", -1);
                return true;
            }
            catch
            {
                try
                {
                    Connection A = new Connection();
                    conn.CommandTimeout = 10;
                    conn.ConnectionTimeout = 1;
                    conn.ConnectionString = sqlstr;
                    conn.Open(sqlstr, "", "", -1);
                    return true;
                }
                catch (System.Exception e)
                {
                    MessageBox.Show("数据库连接错误!!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    //try { conn.Close(); }
                    //catch { }
                    return false;
                }
            }
         }        public Boolean chaxun(string sql, DataGridView Dg, string bz)
        {
            try
            {
                if (conn.State == 0)
                {
                    if (!_Conn()) { return false; }
                }                AdoRs.Open(sql, conn, CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockUnspecified, -1);                if (!AdoRs.EOF & !AdoRs.BOF)
                {
                    DataTable table = new DataTable();
                    table = Li.RecordsetTODataTable(AdoRs);
                    Dg.DataSource = table;
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.Message );
                return false;
            }        }
    }

解决方案 »

  1.   

    你试试看重写Dispose方法吧,.net没有析构的!
      

  2.   

    还是不太明白,能具体点说吗?怎么重写 Dispose方法 啊
      

  3.   

     在 finally中关闭。
    或者using(SqlConnection con=...){...} ~GF_ADODB()这个要等到GF_ADODB对象被回收才会调用。
    连接释放在这里肯定不行,不能及时释放资源
      

  4.   

    .net没有析构你只要把连接对象在用完后dispose掉就可以了
      

  5.   

    首先.net中有有析构函数的!   
    你的析构函数写的有点问题  
       /// <summary>
            /// 析构函数
            /// </summary>
            ~GF_ADODB()
            {
                try
                {
                    MessageBox.Show("析构");
                    if (AdoRs != null)
                    {
                        AdoRs.Close();
                        AdoRs = null;
                    }
                    if (conn != null)
                    {
                        conn.Close();
                    }           
                }
                catch { }
            }
      

  6.   

    首先.net中有有析构函数的!       
    你的析构函数写的有点问题     
          ///   <summary> 
                    ///   析构函数 
                    ///   </summary> 
                    ~GF_ADODB() 
                    { 
                            try 
                            { 
                                    MessageBox.Show( "析构 "); 
                                   if   (AdoRs   !=   null) 
                                    { 
                                            AdoRs.Close(); 
                                            AdoRs   =   null; 
                                    } 
                                 if   (conn   !=   null) 
                                    { 
                                            conn.Close(); 
                                    }                       
                            } 
                            catch   {   } 
                    }
      

  7.   


    ADODB.Connection里面就没有dispose方法,只有一个Close方法
    然后我在CHAXUN这个函数里边调用Close方法也是不行
            public Boolean chaxun(string sql, DataGridView Dg, string bz)
            {
                try
                {
                    if (conn.State == 0)
                    {
                        if (!_Conn()) { return false; }
                    }                AdoRs.Open(sql, conn, CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockUnspecified, -1);                if (!AdoRs.EOF & !AdoRs.BOF)
                    {
                        DataTable table = new DataTable();
                        table = Li.RecordsetTODataTable(AdoRs);
                        Dg.DataSource = table;
                    if (AdoRs == null)
                    {
                        AdoRs.Close();
                        AdoRs = null;
                    }
                    if (conn == null)
                    {
                        conn.Close();
                    } 
                        return true;
                    }
                    else
                    {
                                       if (AdoRs == null)
                    {
                        AdoRs.Close();
                        AdoRs = null;
                    }
                    if (conn == null)
                    {
                        conn.Close();
                    } 
             return false;
                    }
                }
                catch (System.Exception e)
                {
                    if (AdoRs == null)
                    {
                        AdoRs.Close();
                        AdoRs = null;
                    }
                    if (conn == null)
                    {
                        conn.Close();
                    }                
     MessageBox.Show(e.Message );
                    return false;
                }        }
      

  8.   

    ADODB.Connection没发Close?不报错?
      

  9.   


    在 finally中关闭。
    或者using(SqlConnection con=...){...}~GF_ADODB()这个要等到GF_ADODB对象被回收才会调用。
    连接释放在这里肯定不行,不能及时释放资源
      

  10.   

    你断点调试一下,如果的确执行了Close方法就没有问题了。