I'm not sure if i'm just having a dumb moment here or I generally do not understand but can someone explain to me why this will run:
using System.Data.SqlClient;
using System.Configuration;
public void SomeMethod()
{
using(SqlConnection dbConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString))
{
using(SqlCommand qry = new SqlCommand(someSqlString, dbConnection))
{
dbConnection.Open();
using(SqlDataReader reader = qry.ExecuteReader())
{
...
}
}
}
}
and this will not run throwing an InvalidOperationException (The ConnectionString property has not been initialized) when it hits dbConnection.Open():
using System.Data.SqlClient;
using System.Configuration;
private SqlConnection dbConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString);
public void SomeMethod()
{
using(dbConnection)
{
using(SqlCommand qry = new SqlCommand(someSqlString, dbConnection))
{
dbConnection.Open();
using(SqlDataReader reader = qry.ExecuteReader())
{
...
}
}
}
}