Solved

VB.NET: SqlDataReader + DataTable : Copying returned multiple recordset into a single recordsets

Posted on 2011-02-15
3
1,264 Views
Last Modified: 2014-11-12
In order to improve performance on my SQL Azure database, I want to combine several queries together.

SELECT * FROM Table1;
SELECT * FROM Table2;
SELECT * FROM Table3;

I have three DataTable objects that I would like to copy the returned data.

Dim dtTable1 As New DataTable
Dim dtTable2 As New DataTable
Dim dtTable3 As New DataTable

When I load the DataTable object with the recordset, it appears to include all returned recordsets, rather than just one.

Example:

dtTable1.Load(Rs)

Rs.NextResult
dtTable2.Load(Rs)

Rs.NextResult
dtTable3.Load(Rs)

After several recordsets are copied into the DataTables, it errors.

A first chance exception of type 'System.InvalidOperationException' occurred in System.Data.dll

If I run the queries separately, I don't receive the error.  I am trying to accomplish:

  o Single, quick query to the Azure server using 1 query
  o Update the DataTable objects with the returned recordsets, separately

Is there a way to "copy" out a single recordset to a new object without the other recordsets included?

0
Comment
Question by:Officedoxs
3 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 34904536
Don't you have the possibility to query the tables at once by using a union ?

>> A first chance exception of type 'System.InvalidOperationException' occurred in System.Data.dll

Where do you get that error ?
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 34906047
Given that the Load method of DataTable internal calls the NextResult,
http://platinumdogs.wordpress.com/2009/05/14/sqldatareader-datatable-and-multiple-resultsets/

you have to use an alternative. I think you have two options

1) Use DataSet.Load
2) Use a DataAdapter.Fille(DataSet) which would create separate DataTable objects for each resultset.
0
 

Author Closing Comment

by:Officedoxs
ID: 34908464
From the referenced link, I can use the DataTable object successfully.  It's just that I need to remove the Rs.NextResult lines in my code, the .Load does it automatically.
0

Featured Post

Why do Marketing keep bothering you?

Is your marketing department constantly asking for new email signature updates? Are they requesting a different design for every department? Do they need yet another banner added? Don’t let it get you down! There is an easy way to manage all of these requests...

Join & Write a Comment

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now