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();
精彩评论