new SqlConnection(connectionString)
creates a new SqlConnection instance against the attached connection string.
SqlConnection cn = ...
assigns it a new local variable cn (attached to the using statement), which contains the constructed connection object.
using(...)
It is a using statement - it ensures that the Dispose() -d connection is at the end, even if an exception is thrown (in this case, Dispose() means closing / releasing it to the pool, etc.)
All code is essentially:
{ // this { } scope is to limit the "cn" SqlConnection cn = new SqlConnection(connectionString); try { // the body of the using block ... } finally { // dispose if not null if(cn != null) { cn.Dispose(); } } }
source share