开发者

Enterprise library 5.0 Close active connection forcefully

开发者 https://www.devze.com 2023-02-26 17:00 出处:网络
How can I close Database con开发者_如何学Gonection forcefully? The sample code I\'m using to create connection is:

How can I close Database con开发者_如何学Gonection 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..

}


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();
0

精彩评论

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