Call SqlCommand.ExecuteReader inside another SqlCommand.ExecuteRea

S

Stephany Young

Any SqlConnection object can only have one active SqlDataReader at a time.

To do what you need to do you need to have a second SqlConnection object for
the inner SqlDataReader.

I do not think that a 'transaction' can span multiple connections.

It appears that you are only 'reading' so there does not appear to be any
need for a transaction.
 
S

Stephany Young

Que?

Do you mean that you are concerned that a row of interest in table2 could be
updated after you execute the select on table1 but before you execute the
inner select on table2?

Try, using one SqlDataReader

begin transaction;
select * from table1;
select * from table2 where pk in (select fk from table1);
end transaction;

When you have finished cycling through the SqlDataReader (table1) then
execute it's NextResult method and cycle through it again for table2.

You can do it one way or you can do it the other way, but you can't do it
both ways at once.
 
S

Stephany Young

That's the whole idea.

For the second select you get all the rows from table2 for all the rows
selected in table1.

It's not rocket science to write a couple of lines of code to match them up.
 
M

Miha Markic [MVP C#]

Hi David,

Why don't you create a stored procedure instead?
You can also check out MARS (Multiple Active Result Sets) capability - I am
not sure whether it will help you or not.
 
W

WenYuan Wang

Hi Dave,

Thanks Miha and Stephany,
In ADO.net 2.0, Multiple Active Result Sets (MARS) is a new feature that
works with SQL Server 2005 to allow the execution of multiple batches on a
single connection. I think this is maybe what you need.

You can get more detailed information from the following website.
http://msdn2.microsoft.com/en-us/library/cfa084cz.aspx

Please feel free to reply me if you have any further questions or concerns
on this, and we will follow up.
I'm glad to work with you.

Hope this will help!
Best Regards,
Wen Yuan
Microsoft Online Community Support
===============================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
===============================
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top