J
JonOfAllTrades
I'm sure this has been answered before, but I couldn't find the right search
terms.
We've all seen how a single SqlConnection can only serve one SqlDataReader
at a time. However, often one needs to query within a queried loop.
Typically, I'd create a second SqlConnection called "innerLoopConnection" or
similar. Rather than handling this manually, I'd like to modify my
ExecuteReader wrapper fn to check to see if the SqlConnection is in use, and
if it is, create a new SqlConnection on the spot. Something like this:
SqlConnection reusedConnection;
....
SqlDataReader ReadQuery(string query)
{
if (reusedConnection.State != System.Data.ConnectionState.Open) return
ReadQueryTempConnection(query);
//else
SqlCommand command = new SqlCommand(query, reusedConnection);
return command.ExecuteReader();
}
However, SqlConnection.State seems to be largely NYI. Is there any other
way to check to see if a connection is in a useable state? At the moment,
I'm catching InvalidOperationException and using that as a cue to use
ReadQueryTempConnection(), but it doesn't quite work, it seems that the
pre-existing SqlDataReader is killed by the _attempt_ to reuse its
SqlConnection.
Any ideas? Thanks!
terms.
We've all seen how a single SqlConnection can only serve one SqlDataReader
at a time. However, often one needs to query within a queried loop.
Typically, I'd create a second SqlConnection called "innerLoopConnection" or
similar. Rather than handling this manually, I'd like to modify my
ExecuteReader wrapper fn to check to see if the SqlConnection is in use, and
if it is, create a new SqlConnection on the spot. Something like this:
SqlConnection reusedConnection;
....
SqlDataReader ReadQuery(string query)
{
if (reusedConnection.State != System.Data.ConnectionState.Open) return
ReadQueryTempConnection(query);
//else
SqlCommand command = new SqlCommand(query, reusedConnection);
return command.ExecuteReader();
}
However, SqlConnection.State seems to be largely NYI. Is there any other
way to check to see if a connection is in a useable state? At the moment,
I'm catching InvalidOperationException and using that as a cue to use
ReadQueryTempConnection(), but it doesn't quite work, it seems that the
pre-existing SqlDataReader is killed by the _attempt_ to reuse its
SqlConnection.
Any ideas? Thanks!