I have a method that opens a DataReader. When obtaining the DataReader via the SqlCommand.ExecuteReader method I use the command behavior "CommandBehavior.CloseConn
ection" to make sure the connection is closed when the reader is closed.
But what happens if an exception iccurs during the "SqlCommand.ExecuteReader"
statement? What happens to the Sql Connection? Do I need to place a SqlConnection.Close() explicitly in the "try .. catch .. finally" so that SqlConnection is closed properly? (Example below)
SqlDataReader sdr = null;
string sql = <MY SQL STATEMENT>;
sqlConn = new SqlConnection(this.SqlConnectionString);
SqlCommand sqlCmd = new SqlCommand(sql, sqlConn);
sqlCmd.CommandType = CommandType.Text;
sdr = sqlCmd.ExecuteReader(CommandBehavior.CloseConnection);
catch (Exception ex)
<HANDLE EXCEPTION HERE>
if (sqlConn != null && sqlConn.State == ConnectionState.Open)