Solved

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

Posted on 2011-02-15
3
1,271 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Select JOIN table 2 24
Sql query 107 30
ms sql last 8 weeks as columns 5 29
SQL Pivot add row totals 2 16
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Healthcare organizations in the United States must adhere to the guidance of both the HIPAA (Health Insurance Portability and Accountability Act) and HITECH (Health Information Technology for Economic and Clinical Health Act) for securing and protec…
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.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

863 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

26 Experts available now in Live!

Get 1:1 Help Now