开发者

Using Dispose() method with SQL-Server Compact Edition 3.5

开发者 https://www.devze.com 2023-02-16 01:55 出处:网络
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:

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();
  开发者_JAVA百科  }

    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


Just use the using statement as it automatically calls the Dispose() on the specified object.

0

精彩评论

暂无评论...
验证码 换一张
取 消