Architecturally, this is a bad idea. First, consider that you should not use
the DataReader as a portal to a server-side cursor--because it's not. Until
the DataReader has read all of the rows returned by the SELECT, the SQL
engine is holding resources and (potentially) blocking other users. This is
bad.
Use the DataReader to open a channel to the rowset. Fetch the rows as
quickly into RAM -- as with the DataTable Load method. Process the rows in
memory. BUT in a general sense if you are bringing rows to the client for
processing you're (again) missing the point. Rowset processing should be
done ON THE SERVER--not the client. To make bulk changes to rows, you need
to write a stored procedure that runs on the server to massage the data.
--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit
www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
-----------------------------------------------------------------------------------------------------------------------
"JonOfAllTrades" <(E-Mail Removed)> wrote in message
news:C3A1D39B-E64B-4092-BA84-(E-Mail Removed)...
> 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!