Closing connections with ExecuteReader, ExecuteScalar... without DBCommand...

Topics: Data Access Application Block
Oct 28, 2010 at 5:19 PM

I'm executing the commands as follows:


db.ExecuteNonQuery("spXXX...", parameter)

db.ExecuteScalar("spXXX.....", parameters)

(without using DBCommand..!)

However, I dont know how to close the connection. I've only found examples that use DBCommand which allowed to close connections explicitly.


Oct 28, 2010 at 11:25 PM

You don't need to close the connection, EntLib automatically closes it even in cases when you used a DbCommand object.  You only need to close it when you instantiated a DbConnection prior to executing a command.  In cases when you get an instance of IDataReader, you also need to close the data reader explicitly.


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.