Akhil
Akhil

Reputation: 1441

Enterprise library 5.0 Close active connection forcefully

How can I close Database connection forcefully?

The sample code I'm using to create connection is:

class Customer{
     private readonly Database _db;
      public Customer(){
            _db = = DatabaseFactory.CreateDatabase(_userSettings.ConnstringName);
       }

   .. stuff to use this connection..

}

Upvotes: 3

Views: 1879

Answers (1)

Joe Ratzer
Joe Ratzer

Reputation: 18549

Put the code (.. stuff to use this connection..) inside a using block, which will ensure the connection is closed. For example:

using (DbCommand command = _db.GetStoredProcCommand(sprocName, parameters))    
{

and:

using (IDataReader rdr = _db.ExecuteReader(command))
{

Using blocks are a nice way of ensuring resources are closed properly:

The using statement allows the programmer to specify when objects that use resources should release them.

Otherwise, you have to explicitly call the Close() method on the connection object:

if (command.Connection.State == ConnectionState.Open)
            command.Connection.Close();

Upvotes: 1

Related Questions