PC Review


Reply
Thread Tools Rate Thread

C# and many SQLDataReaders

 
 
=?Utf-8?B?Q2hyaXM=?=
Guest
Posts: n/a
 
      28th Aug 2006
hi,
I need to use a few SQLDataReader objects in my application. I need to do
something like that:

System.Data.SqlClient.SqlConnection conn = new SqlConnection(string);
conn.Open();
System.Data.SqlClient.SqlCommand com = new SQLCommand(string, conn);
SqlDataReader data1 = com.ExecuteReader();
while(data1.Read())
{
//read a new SQLDataReader
}
conn.Close();


the problem is in the loop. I need to read a new datareader for each data in
data1

the problem is that I must create a new connection to database and associate
that with a new sqldatareader. It is stupid - for any new sqldatareader a new
connection to db must be established.

If I try to do:

com.CommandText="new statement";
SQLDataReader data2 = com.ExecuteReader();

I am thrown the error (something like this):
There is already an open dataset associated with this connection. Close this
first
 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      28th Aug 2006
Unless your connection supports multiple readers (SQL-Server 2005?),
then do what it says and use a second connection. Alternatively,
arrange your data so that it can all be read with a single reader; e.g.
return one grid with both the parent and child data (switching parent
when you spot e.g. a primary key change value), or return multiple
grids, one per parent; personally, I tend to return a single parent
grid and a single child grid, e.g.)

ParentID, ParentColumn1, ParentColumn2,...
10012,x,x,x
12041,x,x,x

ParentID, ChildID, ChildColumn1, ChildColumn2,...
10012,14411,y,y,y
10012,14134,y,y,y
12041,12452,y,y,y

etc

DataReaders can almost all handle multiple grids.

Marc

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question on sqldatareaders =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= Microsoft ASP .NET 6 17th Aug 2007 02:22 PM
What's the recommended way of using to SqlDataReaders in a loop? Rico Alexander Microsoft ASP .NET 3 17th Oct 2003 11:22 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:21 AM.