类的释放

sgp1201 2004-04-29 02:50:38
我写一个类,第一次写的,由早是连接SQL SERVER和执行SQL语句的,所以在运行多次以后没有释放,IIS的线程池会被占完,所以需要加上释放功能,但我不知道怎么才能释放,所以请大哥们帮看看,我这个类应该如何写!
完整代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Configuration;

using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient ;
//using System.Data.OleDb ;


namespace Vekong.Classes
{
/// <summary>
/// kqOperDB类,用于查询、插入、更新、删除数据表
/// Author:史贵平
/// </summary>
public class kqOperDB
{
private string sSql ; //查询SQL语句
private string sqlUpdate ; //更新SQL语句
private string sTable ; //数据集表名

kqOperDB()
{
//ShowMessage("构造");
}

~kqOperDB()
{
//ShowMessage("析构");
Dispose (false);
}

public void Dispose()
//供程序员显式调用的Dispose方法
{
Dispose(true);
//调用带参数的Dispose方法,释放托管和非托管资源
System.GC.SuppressFinalize(this);
//手动调用了Dispose释放资源,那么析构函数就是不必要的了,这里阻止GC调用析构函数
}
protected void Dispose(bool disposing)
//protected的Dispose方法,保证不会被外部调用。
//传入bool值disposing以确定是否释放托管资源
{
if (disposing)
{
//在这里加入清理"托管资源"的代码,应该是xxx.Dispose();
//这里释放哪些


}
// 在这里加入清理"非托管资源"的代码
//这里释放哪些

}

public string kqTable
{
get
{
return sTable;
}
set
{
sTable = value;
}
}

public string kqSelSql
{
get
{
return sSql;
}
set
{
sSql = value;
}
}

public string kqOpeSql
{
get
{
return sqlUpdate ;
}
set
{
sqlUpdate = value ;
}
}

public void ClearSql()
{
sSql = null ;
}

public SqlConnection CreateConn()
{
string DSN ;
DSN = ConfigurationSettings.AppSettings["DSN"];
SqlConnection Conn = new SqlConnection(DSN);
return Conn;

/*SqlConnection AppConn = new SqlConnection();
AppConn Conn = new AppConn();
AppConn = Conn.CreateConn();
AppConn.Open();
return AppConn ;
*/
}

public DataSet SelDb()
{
SqlConnection AppConn = CreateConn() ;
AppConn.Open();

string Sql = kqSelSql ;
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(kqSelSql,AppConn);
da.Fill(ds,kqTable.Trim());
//AppConn.Close();
return ds ;

}

/*private void ShowMessage(string message)
{
Response.Write("<script language=javascript>");
Response.Write("alert(\""+message+"\");");
Response.Write("</script>");
}*/

public void OpeDb()
{
SqlConnection AppConn = CreateConn() ;
AppConn.Open();

string Sql =kqOpeSql ;
SqlCommand cmdSave = new SqlCommand(Sql,AppConn);
cmdSave.CommandType = CommandType.Text;
cmdSave.ExecuteNonQuery();
//AppConn.Close();
}

}

}
...全文
56 12 点赞 打赏 收藏 举报
写回复
12 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
luckyfool 2004-04-29
// Design pattern for the base class.
// By implementing IDisposable, you are announcing that instances
// of this type allocate scarce resources.
public class BaseResource: IDisposable
{
// Pointer to an external unmanaged resource.
private IntPtr handle;
// Other managed resource this class uses.
private Component Components;
// Track whether Dispose has been called.
private bool disposed = false;

// Constructor for the BaseResource object.
public BaseResource()
{
// Insert appropriate constructor code here.
}

// Implement IDisposable.
// Do not make this method virtual.
// A derived class should not be able to override this method.
public void Dispose()
{
Dispose(true);
// Take yourself off the Finalization queue
// to prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}

// Dispose(bool disposing) executes in two distinct scenarios.
// If disposing equals true, the method has been called directly
// or indirectly by a user's code. Managed and unmanaged resources
// can be disposed.
// If disposing equals false, the method has been called by the
// runtime from inside the finalizer and you should not reference
// other objects. Only unmanaged resources can be disposed.
protected virtual void Dispose(bool disposing)
{
// Check to see if Dispose has already been called.
if(!this.disposed)
{
// If disposing equals true, dispose all managed
// and unmanaged resources.
if(disposing)
{
// Dispose managed resources.
Components.Dispose();
}
// Release unmanaged resources. If disposing is false,
// only the following code is executed.
CloseHandle(handle);
handle = IntPtr.Zero;
// Note that this is not thread safe.
// Another thread could start disposing the object
// after the managed resources are disposed,
// but before the disposed flag is set to true.
// If thread safety is necessary, it must be
// implemented by the client.

}
disposed = true;
}

// Use C# destructor syntax for finalization code.
// This destructor will run only if the Dispose method
// does not get called.
// It gives your base class the opportunity to finalize.
// Do not provide destructors in types derived from this class.
~BaseResource()
{
// Do not re-create Dispose clean-up code here.
// Calling Dispose(false) is optimal in terms of
// readability and maintainability.
Dispose(false);
}

// Allow your Dispose method to be called multiple times,
// but throw an exception if the object has been disposed.
// Whenever you do something with this class,
// check to see if it has been disposed.
public void DoSomething()
{
if(this.disposed)
{
throw new ObjectDisposedException();
}
}
}

// Design pattern for a derived class.
// Note that this derived class inherently implements the
// IDisposable interface because it is implemented in the base class.
public class MyResourceWrapper: BaseResource
{
// A managed resource that you add in this derived class.
private ManagedResource addedManaged;
// A native unmanaged resource that you add in this derived class.
private NativeResource addedNative;
private bool disposed = false;

// Constructor for this object.
public MyResourceWrapper()
{
// Insert appropriate constructor code here.
}

protected override void Dispose(bool disposing)
{
if(!this.disposed)
{
try
{
if(disposing)
{
// Release the managed resources you added in
// this derived class here.
addedManaged.Dispose();
}
// Release the native unmanaged resources you added
// in this derived class here.
CloseHandle(addedNative);
this.disposed = true;
}
finally
{
// Call Dispose on your base class.
base.Dispose(disposing);
}
}
}
}

// This derived class does not have a Finalize method
// or a Dispose method without parameters because it inherits
// them from the base class.
实现 Close 方法
对于类型来说,若调用 Close 方法比调用 Dispose 方法更简易,则可以向基类型添加一个公共 Close 方法。Close 方法又调用没有参数的 Dispose 方法,该方法可以执行正确的清理操作。下面的代码示例阐释了 Close 方法。

// Do not make this method virtual.
// A derived class should not be allowed
// to override this method.
public void Close()
{
// Calls the Dispose method without parameters.
Dispose();
}
  • 打赏
  • 举报
回复
bjy_ly 2004-04-29
AppConn.Close();
AppConn = null;
  • 打赏
  • 举报
回复
luckyfool 2004-04-29
// Design pattern for the base class.
// By implementing IDisposable, you are announcing that instances
// of this type allocate scarce resources.
public class BaseResource: IDisposable
{
// Pointer to an external unmanaged resource.
private IntPtr handle;
// Other managed resource this class uses.
private Component Components;
// Track whether Dispose has been called.
private bool disposed = false;

// Constructor for the BaseResource object.
public BaseResource()
{
// Insert appropriate constructor code here.
}

// Implement IDisposable.
// Do not make this method virtual.
// A derived class should not be able to override this method.
public void Dispose()
{
Dispose(true);
// Take yourself off the Finalization queue
// to prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}

// Dispose(bool disposing) executes in two distinct scenarios.
// If disposing equals true, the method has been called directly
// or indirectly by a user's code. Managed and unmanaged resources
// can be disposed.
// If disposing equals false, the method has been called by the
// runtime from inside the finalizer and you should not reference
// other objects. Only unmanaged resources can be disposed.
protected virtual void Dispose(bool disposing)
{
// Check to see if Dispose has already been called.
if(!this.disposed)
{
// If disposing equals true, dispose all managed
// and unmanaged resources.
if(disposing)
{
// Dispose managed resources.
Components.Dispose();
}
// Release unmanaged resources. If disposing is false,
// only the following code is executed.
CloseHandle(handle);
handle = IntPtr.Zero;
// Note that this is not thread safe.
// Another thread could start disposing the object
// after the managed resources are disposed,
// but before the disposed flag is set to true.
// If thread safety is necessary, it must be
// implemented by the client.

}
disposed = true;
}

// Use C# destructor syntax for finalization code.
// This destructor will run only if the Dispose method
// does not get called.
// It gives your base class the opportunity to finalize.
// Do not provide destructors in types derived from this class.
~BaseResource()
{
// Do not re-create Dispose clean-up code here.
// Calling Dispose(false) is optimal in terms of
// readability and maintainability.
Dispose(false);
}

// Allow your Dispose method to be called multiple times,
// but throw an exception if the object has been disposed.
// Whenever you do something with this class,
// check to see if it has been disposed.
public void DoSomething()
{
if(this.disposed)
{
throw new ObjectDisposedException();
}
}
}

// Design pattern for a derived class.
// Note that this derived class inherently implements the
// IDisposable interface because it is implemented in the base class.
public class MyResourceWrapper: BaseResource
{
// A managed resource that you add in this derived class.
private ManagedResource addedManaged;
// A native unmanaged resource that you add in this derived class.
private NativeResource addedNative;
private bool disposed = false;

// Constructor for this object.
public MyResourceWrapper()
{
// Insert appropriate constructor code here.
}

protected override void Dispose(bool disposing)
{
if(!this.disposed)
{
try
{
if(disposing)
{
// Release the managed resources you added in
// this derived class here.
addedManaged.Dispose();
}
// Release the native unmanaged resources you added
// in this derived class here.
CloseHandle(addedNative);
this.disposed = true;
}
finally
{
// Call Dispose on your base class.
base.Dispose(disposing);
}
}
}
}

// This derived class does not have a Finalize method
// or a Dispose method without parameters because it inherits
// them from the base class.
实现 Close 方法
对于类型来说,若调用 Close 方法比调用 Dispose 方法更简易,则可以向基类型添加一个公共 Close 方法。Close 方法又调用没有参数的 Dispose 方法,该方法可以执行正确的清理操作。下面的代码示例阐释了 Close 方法。
// Do not make this method virtual.
// A derived class should not be allowed
// to override this method.
public void Close()
{
// Calls the Dispose method without parameters.
Dispose();
}
  • 打赏
  • 举报
回复
luckyfool 2004-04-29
下面的代码示例旨在阐释如何为封装了非托管资源的类实现 Dispose 方法的一种可能的设计模式。因为该模式是在整个 .NET Framework 中实现的,所以您可能会发现它十分便于使用。但是,这不是 Dispose 方法唯一可能的实现。

资源类通常是从复杂的本机类或 API 派生的,而且必须进行相应的自定义。使用这一代码模式作为创建资源类的一个起始点,并根据封装的资源提供必要的自定义。不能编译该示例,也不能将其直接用于应用程序。
在此示例中,基类 BaseResource 实现可由类的用户调用的公共 Dispose 方法。而该方法又调用 virtual Dispose(bool disposing) 方法(Visual Basic 中为 virtual Dispose(disposing As Boolean))。根据调用方的标识传递“真”或“假”。以虚 Dispose 方法为对象执行适当的清理代码。
Dispose(bool disposing) 以两种截然不同的方案执行。如果“处置”等于“真”,则该方法已由用户的代码直接调用或间接调用,并且可处置托管资源和非托管资源。如果“处置”等于“假”,则该方法已由运行库从终结器内部调用,并且只能处置非托管资源。因为终结器不会以任意特定的顺序执行,所以当对象正在执行其终止代码时,不应引用其他对象。如果正在执行的终结器引用了另一个已经终止的对象,则该正在执行的终结器将失败。
基类提供的 Finalize 方法或析构函数在未能调用 Dispose 的情况下充当防护措施。Finalize 方法调用带有参数的 Dispose 方法,同时传递“假”。不应在 Finalize 方法内重新创建 Dispose 清理代码。调用 Dispose(false) 可以优化代码的可读性和可维护性。
类 MyResourceWrapper 阐释如何使用 Dispose 从实现资源管理的类派生。MyResourceWrapper 重写 virtual Dispose(bool disposing) 方法并为其创建的托管和非托管资源提供清理代码。MyResourceWrapper 还对其基类 BaseResource 调用 Dispose 以确保其基类能够适当地进行清理。请注意,派生类 MyResourceWrapper 没有不带参数的 Finalize 方法或 Dispose 方法,因为这两个方法从基类 BaseResource 继承它们。
注意 此示例中的 protected Dispose(bool disposing) 方法不强制线程安全,因为无法从用户线程和终结器线程同时调用该方法。另外,使用 BaseResource 的客户端应用程序应从不允许多个用户线程同时调用 protected Dispose(bool disposing) 方法。应用程序或类库的设计原则为:应用程序或类库应只允许一个线程拥有资源的生存期,以及在不再需要资源时调用 Dispose。根据资源的不同,在处置资源时进行异步线程访问可能会带来安全风险。开发人员应仔细检查自己的代码,以确定最佳的方法来强制线程安全。
  • 打赏
  • 举报
回复
sgp1201 2004-04-29
如何继承Idisposable这个呢,让各位见笑了!
  • 打赏
  • 举报
回复
luckyfool 2004-04-29
打开或关闭数据库连接之前请先判断状态
AppConn.State


类型的 Dispose 方法应该释放它拥有的所有资源。它还应该通过调用其父类型的 Dispose 方法释放其基类型拥有的所有资源。该父类型的 Dispose 方法应该释放它拥有的所有资源并同样也调用其父类型的 Dispose 方法,从而在整个基类型层次结构中传播该模式。要确保始终正确地清理资源,Dispose 方法应该可以被多次安全调用而不引发任何异常。

  • 打赏
  • 举报
回复
sgp1201 2004-04-29
并且加了以后,运行提示:
不可访问“vekong.classes.kqoperdb.kqoperdb()”,因为它受保护级别限制!
我是加在
da.Fill(ds,kqTable.Trim());
//AppConn.Close();(这里)
return ds ;
  • 打赏
  • 举报
回复
vzxq 2004-04-29
关注,UP
  • 打赏
  • 举报
回复
sgp1201 2004-04-29
这句不行!
AppConn.Close();
AppConn.Dispose();
是不是定义的问题!
  • 打赏
  • 举报
回复
tnt8csdn2000 2004-04-29
appConn.dispose()
  • 打赏
  • 举报
回复
bjy_ly 2004-04-29
AppConn.Close();
AppConn.Dispose();
  • 打赏
  • 举报
回复
tnt8csdn2000 2004-04-29
继承Idisposable,用IDisposable.Dispose 方法释放
  • 打赏
  • 举报
回复
相关推荐
发帖
.NET社区
加入

5.9w+

社区成员

.NET技术交流专区
申请成为版主
帖子事件
创建了帖子
2004-04-29 02:50
社区公告
暂无公告