简体   繁体   中英

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. 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

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 call Dispose in the Close method of your own class, or, even better you could implement IDisposable .

Implementing IDisposable is highly recommended whenever your class stores resources that need disposing. 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.

Take a look at Implement IDisposable correctly

只需使用using语句,因为它会自动在指定对象上调用Dispose()。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM