Posted: 6/16/2011
Hi All,
I am little bit confuse about the use of the ExecuteReader()'s overlaod method with enum CommandBehavior.CloseConnection
like if we are using
using (SqlDataReader reader = command.ExecuteReader()) { }
in using reader automatically dispose ,then what is use of the Pass CommandBehavior.CloseConnection
Thanks
Vijendra
Hi Vijendra,
When you use using to automatically dispose the objects inside the using block, so CommandBehavior.CloseConnection may not affect anything. Anyway, sometimes you may use it out of using block when Data Reader is open for reading data, this argument gives the ability once the Data Reader is closed, the associated connection string will be also closed.
So, I see not much use of it when you use using blocks, but it is very nice feature when you perform operations with Data Reader outside using block.
Hope this helps.Hajan
Thanks Hajan