在 SQL-Server Compact Edition 3.5 中使用 Dispose() 方法
我在我的 C# 应用程序中使用 Microsoft 的 SQL-Server Compact Edition 3.5。 SqlCeConnection
将由自己的 Connection 类封装:
using System;
using System.Data.SqlServerCe;
class Connection
{
public Connection()
{
m_connection = new SqlCeConnection(connectionString);
}
public void Open()
{
m_connection.Open();
}
public void Close()
{
m_connection.Close();
}
private SqlCeConnection m_connection;
}
所以我的问题是:我是否必须调用 SqlCeConnection 实例的 Dispose() 方法,或者是否可以在我的类中实现 IDisposable 接口?斯特凡
I'm using Microsoft's SQL-Server Compact Edition 3.5 in my C# application. The SqlCeConnection
will be encapsulated by an own Connection class:
using System;
using System.Data.SqlServerCe;
class Connection
{
public Connection()
{
m_connection = new SqlCeConnection(connectionString);
}
public void Open()
{
m_connection.Open();
}
public void Close()
{
m_connection.Close();
}
private SqlCeConnection m_connection;
}
So my Question is: Do I have to call the Dispose() method of the SqlCeConnection instance or may implement the IDisposable interface in my class?
Stefan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
鉴于您使用的是一次性对象,您必须确保在不再需要资源时调用其
Dispose
方法。您有多种选择:您可以在自己的类的Close
方法中调用Dispose
,或者更好的是,您可以实现IDisposable
。每当您的类存储需要处置的资源时,强烈建议实现
IDisposable
。这将允许您的类的用户使用using
模式或自行调用Dispose
,确保始终尽快释放资源。查看正确实现 IDisposable
Given that you are using an object that is disposable, you have to make sure that you call its
Dispose
method when the resource is no longer needed. You have several choices: you could callDispose
in theClose
method of your own class, or, even better you could implementIDisposable
.Implementing
IDisposable
is highly recommended whenever your class stores resources that need disposing. This will allow users of your class to use theusing
pattern or callDispose
themselves, ensuring that resources are always freed as soon as possible.Take a look at Implement IDisposable correctly
只需使用
using
语句,因为它会自动调用指定对象上的 Dispose()。Just use the
using
statement as it automatically calls the Dispose() on the specified object.