Microsoft KB Archive/309490

= How To Handle Multiple Results by Using the DataReader in Visual Basic .NET =

Article ID: 309490

Article Last Modified on 5/13/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1 Service Pack 1
 * Microsoft ADO.NET 1.1
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition

-



This article was previously published under Q309490



For a Microsoft Visual C# .NET version of this article, see 311274.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.Data.OleDb
 * System.Data.SqlClient

IN THIS TASK
SUMMARY
 * Description of the Technique
 * Requirements
 * Create Project and Add Code

REFERENCES



SUMMARY
This article contains a generic function that you can use to process multiple recordsets and other messages that are returned from stored procedures or the execution of batch SQL statements.

back to the top

Description of the Technique
ActiveX Data Objects (ADO) can receive five different types of data from the server:
 * Recordset
 * Number of records that are modified by an action query (such as INSERT, UPDATE, DELETE, or SELECT INTO)
 * Informational message or warning
 * Error message
 * Stored procedure return values and output parameters

When you read the results of a batch SQL statement, you can use the NextResult method to position the DataReader at the next result in the resultset.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, or Windows NT 4.0 Server
 * Microsoft Visual Studio .NET
 * Microsoft SQL Server 7.0 or later

This article assumes that you are familiar with the following topics:
 * Visual Studio .NET
 * ADO.NET fundamentals and syntax

back to the top

Create Project and Add Code
This sample code uses the Authors table of the Pubs sample database.   Paste the following statements into the SQL Query Analyzer tool or the ISQL utility: CREATE PROC MyProc AS SELECT * FROM Authors SELECT * FROM Authors WHERE State = 'CA' GO  Open Visual Studio .NET. Create a new Visual Basic Windows Application project. Make sure that your project contains a reference to the System.Data namespace, and add a reference to this namespace if it does not. Place a Command button on Form1. Change the Name property of the button to btnTest, and change the Text property to Test .  Use the Imports statement on the System, System.Data.OleDb, and System.Data.SqlClient namespaces so that you are not required to qualify declarations in those namespaces later in your code. Add the following code to the General Declarations section of Form1: Imports System Imports System.Data.OleDb Imports System.Data.SqlClient   Add the following code after the &quot;Windows Form Designer generated code&quot; region in the Code window.

Note You must change User ID and password = to the correct values before you run this code. Make sure that User ID has the appropriate permissions to perform this operation on the database. Private Sub btnTest_Click(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles btnTest.Click Dim myConnString As String = _ &quot;User ID= ;Password= ;Initial Catalog=pubs;Data Source=myServer&quot; Dim myConnection As New SqlConnection(myConnString) Dim myCommand As New SqlCommand Dim myReader As SqlDataReader

myCommand.CommandType = CommandType.StoredProcedure myCommand.Connection = myConnection myCommand.CommandText = &quot;MyProc&quot; Dim RecordCount As Integer

Try myConnection.Open myReader = myCommand.ExecuteReader While myReader.Read 'Write logic to process data for the first result. RecordCount = RecordCount + 1 End While MessageBox.Show(&quot;Total number of Authors: &quot; & RecordCount.ToString)

myReader.NextResult RecordCount = 0

While myReader.Read 'Write logic to process data for the second result. RecordCount = RecordCount + 1 End While MessageBox.Show(&quot;Authors from California: &quot; & RecordCount.ToString) Catch ex As Exception MessageBox.Show(ex.ToString) Finally myConnection.Close End Try End Sub </li> Modify the connection string (myConnString) as appropriate for your environment.</li> Save your project. On the Debug menu, click Start to run your project.</li> Click Test. Notice that the messages boxes display the data that the stored procedure returns.</li></ol>

back to the top