析构函数中调用 Dispose 报错 :Internal .Net Framework Data Provider error 1.[非原创]

搜索MSDN的资源能够找到答案: sql

原文以下
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=473449&SiteID=1 
如下是关于SqlDataReader.CLose()方法的解释: 
http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.close.aspx 
注意这段文字: 
Do not call Close or Dispose on a Connection, a DataReader, or any other managed object in the Finalize method of your class. In a finalizer, you should only release unmanaged resources that your class owns directly. If your class does not own any unmanaged resources, do not include a Finalize method in your class definition. (不要再析构函数中调用Connection, DataReader,或其余托管对象的Close()或Dispose()方法,析构函数中应当只是放类的非托管成员,若是类中不包含非托管成员不要建立析构函数。)安全

这类问题一般因为跨线程访问了非线程安全的对象,即使你没有显示声明一个线程,可是CLR内部会有独立的线程。函数

建议直接实现 IDisposable 接口并在使用完对象以后调用Dispose方法便可(须要释放的对象本身要在Dispose方法中实现)线程

相关文章
相关标签/搜索