[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-02-15
3
Medium Priority
?
1,335 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Considering cloud tradeoffs and determining the right mix for your organization.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

834 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