G
Guest
If I close the connection, would the reader already open on the connection be
automatically closed without explicitly closing the reader? I have an
ASP.NET app that on a daily basis generates 'there is already an open
DataReader ...' exception. I search the code to see where I am calling a
DataReader explicitly. I found this piece of code:
using(SqlConnection conn = new SqlConnection...)
{
using(SqlCommand cmd = new SqlCommand...)
{
....
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while(rdr.Read())
{
...
}
rdr.Close();
....
}
}
If an exception occurs after acquiring a reader but before closing the
reader explicitly, the connection object should be properly closed and
disposed since I am using 'using' statement. I am assuming that the active
reader on the connection should be closed with the connection as well right?
or Do I still need to catch the exception and close the reader explicitly by
rdr.Close();
?
Thanks.
automatically closed without explicitly closing the reader? I have an
ASP.NET app that on a daily basis generates 'there is already an open
DataReader ...' exception. I search the code to see where I am calling a
DataReader explicitly. I found this piece of code:
using(SqlConnection conn = new SqlConnection...)
{
using(SqlCommand cmd = new SqlCommand...)
{
....
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while(rdr.Read())
{
...
}
rdr.Close();
....
}
}
If an exception occurs after acquiring a reader but before closing the
reader explicitly, the connection object should be properly closed and
disposed since I am using 'using' statement. I am assuming that the active
reader on the connection should be closed with the connection as well right?
or Do I still need to catch the exception and close the reader explicitly by
rdr.Close();
?
Thanks.