Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 sql data reader

Author  Topic 

needforhint
Starting Member

12 Posts

Posted - 2009-04-14 : 15:45:43
hello all,
I would like to read results from more sqldatareaders but I can not have more than one sqldatareader opened, I have to call SqlDataReader::Close() before I can execute a sql command for another SqlDataReader instance. What can I do in other to have another SqlDataReader and read from previous one still?

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2009-04-15 : 07:59:27
This is a limitation of the SqlDataReader. It is designed as a fast, forward-only reader which lets you read data one row at a time and process, but you cannot run another query until you close the reader. Alternatives I can think of are:

a) Cache the data locally, close the reader and then do the other query. Or perhaps use SqlDataAdapter and a Dataset. You probably won't like either option because of memory foot-print issues.

b) Move the act of reading a row and the subsequent data access into a stored procedure on the server, thus avoiding the need to intertwine the reading and processing in the client code.

c) Use two threads, one for the SqlDataReader and one for the other query and keep them synchronized via events. A bad idea, and complex code, so, I don't think that is a great option.
Go to Top of Page

needforhint
Starting Member

12 Posts

Posted - 2009-04-15 : 10:23:37
thanks for the answer,
In php sripts there is no problem to execute two different search queries and parallely use both of the results. Why is there such a limitation in C# sql classes?
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2009-04-15 : 14:00:43
Microsoft added MARS - Multiple Active Result Sets to address this issue, but that is not part of the classic ADO.Net. I have very limited experience with MARS, so whatever I say about it is going to be wrong. This page will help you get started: http://msdn.microsoft.com/en-us/library/ms345109(SQL.90).aspx
Go to Top of Page
   

- Advertisement -