Is it opening (and closing) the connection to the SQL server everytime it does that? I assume it would be more efficient to pool the connection to the SQL server, right?
Yes. And no. It is calling Close()
by way of Dispose()
, but it's actually pooling the connections for you in the background, assuming you have not set any properties in the connection string that disallow pooling. You don't do anything for this; just open as late as possible, and close as soon as possible, and let ADO.NET do its thing.
If I were to replace that with Entity Framework 5.0 (with a using (var db = new myDbContext()) { } ) would that result in connection pooling?
The DbContext also uses pooling, because it uses the regular ADO.NET behind the scenes. The code it generates generally follows the advice I gave above: Opens as late as possible, and closes as early as possible - letting ADO.NET handle pooling.