Solved

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

Posted on 2011-02-15
3
1,288 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

In the market for a new backup solution for Windows Server 2016? Follow these guidelines to get the most bang for your buck.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

734 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