Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 311274 - Last Review: May 13, 2007 - Revision: 3.3
How To Handle Multiple Results by Using the DataReader in Visual C# .NET
This article was previously published under Q311274
On This Page
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.
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.
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
Create Project and Add Code
This sample code uses the Authors table of the SQL Server 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
- Start Visual Studio .NET.
- Create a new Windows Application project in Visual C# .NET.
Form1 is added to the project by default.
- 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 using 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:
using System;
using System.Data.OleDb;
using System.Data.SqlClient;
- Add the following code to the btnTest_Click event:
Note You must change the User ID <user
name> account value to an account that has the appropriate permissions to
perform these operations on the database.
String myConnString = "User ID=<username>;password=<strong password>;Initial Catalog=pubs;Data Source=myServer";
SqlConnection myConnection = new SqlConnection(myConnString);
SqlCommand myCommand = new SqlCommand();
SqlDataReader myReader ;
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Connection = myConnection;
myCommand.CommandText = "MyProc";
int RecordCount=0;
try
{
myConnection.Open();
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
//Write logic to process data for the first result.
RecordCount = RecordCount + 1;
}
MessageBox.Show("Total number of Authors: " + RecordCount.ToString());
myReader.NextResult();
RecordCount = 0;
while (myReader.Read())
{
//Write logic to process data for the second result.
RecordCount = RecordCount + 1;
}
MessageBox.Show("Authors from California: " + RecordCount.ToString());
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
myConnection.Close();
}
- Modify the Connection string (myConnString) as appropriate for your environment.
- Save your project. On the Debug menu, click Start to run your project.
- Click Test. Notice that the messages boxes display the data that the stored
procedure returns.
REFERENCES
For
additional information about calling stored procedures, click the article
number below to view the article in the Microsoft Knowledge Base:
306574Â
(http://kbalertz.com/Feedback.aspx?kbNumber=306574/EN-US/
)
How To Call SQL Server Stored Procedures in ASP.NET
For additional information
about error handling, click the article number below to view the article in the
Microsoft Knowledge Base:
308650Â
(http://kbalertz.com/Feedback.aspx?kbNumber=308650/EN-US/
)
How To Obtain Underlying Provider Errors by Using ADO.NET in Visual C# .NET
For additional information about parameters and
stored procedures, click the article number below to view the article in the
Microsoft Knowledge Base:
308621Â
(http://kbalertz.com/Feedback.aspx?kbNumber=308621/EN-US/
)
PRB: Output Parameters Are Not Returned When You Run an ADO.NET Command in Visual C# .NET
For more information on ADO.NET objects and syntax,
refer to the following Microsoft .NET Framework Software Development Kit (SDK)
documentation:
APPLIES TO
- Microsoft ADO.NET (included with the .NET Framework)
- Microsoft ADO.NET 1.1
- Microsoft Visual C# .NET 2002 Standard Edition
- Microsoft Visual C# .NET 2003 Standard Edition
| kbhowtomaster kbsqlclient kbsystemdata KB311274 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
Be the first to leave feedback, to help others about this knowledge base
article.
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please