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;
}
}
{
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;
}
}
解决方案 »
- 分享帖:不使用Word组件,根据Word模板生成新Word(附示例源码)
- 求个控件,要求比较难搞
- C# 调用构造函数问题
- c#中数据库排他
- 救命,mStream.Write()执行完后,内存不能释放.急呀.
- C#中是否有方法可以判断字符串是否为整数和浮点数,着急中。。。。
- 如何解决文件的删除权限问题?
- 为什么在Web下绑定水晶报表中使用参数会报错“丢失参数字段当前值”?
- C# 字符串按长度转泛型List<string> 的实现
- 我们做webservice都用iis做host吧,今天面试有人问我,不用iis,直接监听一个端口,怎么保障安全和影射webservice的webmethod?
- 一个简单的问题,请高手进来看看,谢谢了
- 现在要用OWC11做图表开发,求各位大虾赐教!
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对象不存在}
conn.Dispose();以上二句方法调用是不必要的。而且建议只需执行一次 Close 即可。Close 比 Dipose 好,对于 conn 来说,不信,你查 MSDN,上面有明确说明。及时释放非托管资源,是相当重要的。
可以参考DAAB的代码
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操作。这个问题在某个论坛看过有人讨论,不过当时没有来得及看。
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);