[英]Using Dispose() method with SQL-Server Compact Edition 3.5
I'm using Microsoft's SQL-Server Compact Edition 3.5 in my C# application. 我在C#应用程序中使用Microsoft的SQL-Server Compact Edition 3.5。 The
SqlCeConnection
will be encapsulated by an own Connection class: 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;
}
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? 所以我的问题是: 我必须调用SqlCeConnection实例的Dispose()方法还是可以在类中实现IDisposable接口?
Stefan 斯特凡
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. 假定您使用的是可抛弃的对象,则必须确保在不再需要资源时调用其
Dispose
方法。 You have several choices: you could call Dispose
in the Close
method of your own class, or, even better you could implement IDisposable
. 您有多种选择:您可以在自己的类的
Close
方法中调用Dispose
,或者甚至更好地实现IDisposable
。
Implementing IDisposable
is highly recommended whenever your class stores resources that need disposing. 每当您的班级存储需要处置的资源时,强烈建议实现
IDisposable
。 This will allow users of your class to use the using
pattern or call Dispose
themselves, ensuring that resources are always freed as soon as possible. 这将允许您班级的用户使用
using
模式或自行调用Dispose
,以确保始终尽可能快地释放资源。
Take a look at Implement IDisposable correctly 正确看一下实现IDisposable
只需使用using
语句,因为它会自动在指定对象上调用Dispose()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.