public class myclass : IDisposable
    {
        private OleDbCommand selectCommand;
        private OleDbCommand insertCommand;
        private OleDbCommand updateCommand;
        private OleDbCommand deleteCommand;
        private OleDbCommand storedCommand;
        private OleDbConnection connectionObj;
        private bool isDisposed = false; ............
...........public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(true); // as a service to those who might inherit from us
        }
        
        protected virtual void Dispose(bool disposing)
        {
            if (!isDisposed)
            {
                if (disposing)
                {
                    if (selectCommand != null)
                    {
                        selectCommand.Dispose();
                        selectCommand = null;
                    }
                    if (updateCommand != null)
                    {
                        updateCommand.Dispose();
                        updateCommand = null;
                    }
                    if (insertCommand != null)
                    {
                        insertCommand.Dispose();
                        insertCommand = null;
                    }
                    if (deleteCommand != null)
                    {
                        deleteCommand.Dispose();
                        deleteCommand = null;
                    }
                    if (dataAdapter != null)
                    {
                        dataAdapter.Dispose();
                        dataAdapter = null;
                    }
                }
                if (connectionObj != null)//connectionObj是该放在这吗?
                {
                    connectionObj.Dispose();
                    connectionObj = null;
                }
            }
            isDisposed = true;
        }
}

解决方案 »

  1.   

    OleDbConnection 对象可以在这里被释放,如果你程序中一个方法内使用的同一个OleDbConnection 只有一次比如:
    conn.Open();
    //操作
    conn.Close();
    conn.Disponse();如果一个OleDbConnection实例调用两次那就会出问题比如
    Class a
    {
         void b()
         {
              //代码
              conn.Disponse();
         }     void c()
         {
              //代码
              conn.Disponse();
         }
    }class x
    {
         a Mya = new a();
         a.b();
         a.c();//出错,conn对象被释放,OleDbConnection对象不存在}
      

  2.   

    我想知道OleDbConnection是托管资源还是非托管资源?
      

  3.   

    conn.Close();
    conn.Dispose();以上二句方法调用是不必要的。而且建议只需执行一次 Close 即可。Close 比 Dipose 好,对于 conn 来说,不信,你查 MSDN,上面有明确说明。及时释放非托管资源,是相当重要的。
      

  4.   

    1、CLOSE在内部也是调用Dispose,没有本质区别,只是更加符合程序逻辑2、ADO.NET的与ADO的一个最大区别是取到数据后马上断开,减少连接占用,
       可以参考DAAB的代码
      

  5.   

    至少Connection的Close和Dispose应该是这样子。
    1、Calling Close on a connection object enables the underlying connection to be pooled.2、Calling Dispose on a connection object alleviates the need for you to call Close on it explicitly.(也就是说Dispose会自动帮你调用Close)。It not only ensures that the underlying connection can be pooled, but it also makes sure that allocated resources can now be garbage collected.3、When the open connections fall out of scope, they won't be garbage collected for a relatively long time because the connection object itself doesn't occupy that much memory-and the lack of memory is the sole criterion for the garbage collector to kick in and do its work.In short, Dispose is the best option as it helps garbage collection and connection pooling. Close is second best option as it helps only connection pooling.其它:The ADO.NET uses the Dispose method to clean up unallocated resources. In addition to cleaning up unallocated resources, Dispose calls Close on a connection object and makes it ready for reuse in a connection pool. In addition to calling Close, Dispose does a little more housekeeping work than Close might do. Dispose cleans the internal collections to clear out various settings, such as the connection string on a connection object, so it allows the garbage collector to reuse the memory used by the actual connection object...下面的例子并没有显示执行Connection.Close()
    using(SqlConnection conn = new SqlConnection(connString))
    {
        SqlCommand cmd = new SqlCommand("select * from TABLE", conn);
        
        conn.Open();
        
        SqlDataReader sqlDr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        if(sqlDr.HasRows)
        {
            foreach(DbDataRecord rec in sqlDr)
            {
                dbRecordsHolder.Add(rec); // dbREcordsHoler is an ArrayList
            }
        }
    ] //注意: conn.Dispose is called automatically, 而Dispose会自动调用Close另外一个问题是:CommandBehavior.Close也会自动关闭DataReader及其Connection,也就是说这段代码会有两次Close操作。这个问题在某个论坛看过有人讨论,不过当时没有来得及看。
      

  6.   

    我想说一下
    public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(true); // as a service to those who might inherit from us
            }
    这个方法
    SuppressFinalize
    是指取消为这个类执行Finalize方法
    所以如果显示调用Dispose方法的话就不需要在垃圾收集的时候执行Finalize 析构函数
    这个方法的参数是指要取消的对象
    我想这里调用的话应该是
    GC.SuppressFinalize(this);