VB.Net-Informations.com
   Home      .Net Framework      VB.NET      C#                                                                      About


  How to Multiple Result Sets in ADO.NET

The DataReader Object is a stream-based , forward-only, read-only retrieval of query results from the Data Source, which do not update the data. When the ExecuteReader method in SqlCommand Object execute , it instantiate a SqlClient.SqlDataReader Object.

Dim sqlReader As SqlDataReader = sqlCmd.ExecuteReader()

In some situations we need to pass multiple SQL statements to the Command Object. In this situations the SqlDataReader returns multiple ResultSets also. For retrieveing multiple ResultSets from SqlDataReader we use the NextResult() method of the SqlDataReader.

SqlDataReader.NextResult()

In the following source code demonstrating how to get multiple result sets from SqlDataReader() .

         VB.NET Source Code Download           Print Source Code
         How to Multiple Result Sets in ADO.NET - Download
        
C# Tutorial

Imports System.Data.SqlClient
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connetionString As String
        Dim sqlCnn As SqlConnection
        Dim sqlCmd As SqlCommand
        Dim sql As String

        connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
        sql = "Select top 2 * from product; select top 2 * from ordermaster; select top 2 * from orderdetails"

        sqlCnn = New SqlConnection(connetionString)
        Try
            sqlCnn.Open()
            sqlCmd = New SqlCommand(sql, sqlCnn)
            Dim sqlReader As SqlDataReader = sqlCmd.ExecuteReader()
            While sqlReader.Read()
                MsgBox("From first SQL -  " & sqlReader.Item(0) & "  -  " & sqlReader.Item(1))
            End While

            sqlReader.NextResult()

            While sqlReader.Read()
                MsgBox("From second SQL -  " & sqlReader.Item(0) & "  -  " & sqlReader.Item(1))
            End While

            sqlReader.NextResult()

            While sqlReader.Read()
                MsgBox("From third SQL -  " & sqlReader.Item(0) & "  -  " & sqlReader.Item(1))
            End While

            sqlReader.Close()
            sqlCmd.Dispose()
            sqlCnn.Close()
        Catch ex As Exception
            MsgBox("Can not open connection ! ")
        End Try
    End Sub
End Class

connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
sql = "Select top 2 * from product; select top 2 * from ordermaster; select top 2 * from orderdetails"

You have to replace the string with your realtime variables.


ADO.NET Data Providers Related Contents
*     ADO.NET Connection Object
*     ADO.NET SQL Server Connection
*     ADO.NET OLEDB Connection
*     ADO.NET ODBC Connection
*     ADO.NET Command
*     ADO.NET ExecuteNonQuery in SqlCommand Object
*     ADO.NET ExecuteNonQuery in OleDbCommand Object
*     ADO.NET ExecuteScalar in SqlCommand Object
*     ADO.NET ExecuteScalar in OleDbCommand Object
*     ADO.NET ExecuteReader in SqlCommand Object
*     ADO.NET ExecuteReader in OleDbCommand Object
*     How to ADO.NET DataReader
*     How to ADO.NET SqlDataReader
*     How to ADO.NET OleDbDataReader
*     Getting Schema Informations from SqlDataReader
*     Getting Schema Informations from OleDbDataReader
*     What is DataAdapter
*     What is SqlDataAdapter
*     What is OleDbDataAdapter
*     Vb.NET ExecuteReader and ExecuteNonQuery


   Home      VB.NET      C#
VB.Net Related Topics
*     Microsoft .Net Framework Tutorials
*     VB.NET Language Basics Tutorials
*     VB.NET Program Flow Control Tutorials
*     VB.NET Collections Tutorials
*     VB.NET String Tutorials
*     VB.NET Files Tutorials
*     VB.NET Excel 2007 Tutorials
*     VB.NET Crystal Reports Tutorials
*     VB.NET Communications Tutorial
*     VB.NET ADO.NET Tutorial
*     ADO.NET Data Providers help and Tutorial
*     VB.NET ADO.NET Dataset Tutorial
*     ADO.NET DataAdapter and Dataset
*     VB.NET ADO.NET DataView Tutorial
*     VB.NET Remoting Tutorial
*     VB.NET XML Tutorial
*     VB.NET DataGridView Tutorial
   Home      VB.NET      C#
More Source Code :   
Mail to :  feedback@net-informations.com
  |  Home   |  VB.NET   |  C#   |  SiteMap   |  Terms of Use   |  About   |
net-informations.com (C) 2010
All Rights Reserved. All other trademarks are property of their respective owners.