Library code snippets
How to run through multiple DataReader results
By Edward Tanguay, published on 17 Dec 2004
You can save code by piling SQL statements into one SqlCommand and then getting a DataReader with multiple result sets. This code shows you how to run through these.
/*
using System.Data;
using System.Data.SqlClient;
*/
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["con"]);
con.Open();
SqlCommand cmd = new SqlCommand("SELECT TOP 3 * FROM Employees ORDER BY LastName;SELECT TOP 3 * FROM Employees ORDER BY LastName DESC",con);
SqlDataReader dr = cmd.ExecuteReader();
do {
while(dr.Read()) {
System.Diagnostics.Debug.WriteLine(dr["LastName"]);
}
} while(dr.NextResult());
con.Close();
Related articles
Related discussion
-
High-Performance .NET Application Development & Architecture
by Manjot Bawa (0 replies)
-
An Introduction to VB.NET and Database Programming
by carlosmen (14 replies)
-
How to view MYSQL db data in Microsoft word
by spanish (0 replies)
-
Very slow inserts using SqlCommand.ExecuteNonQuery()
by porchelvi (1 replies)
-
VB.NET Type 'SqlDatabaseException' not defined
by Mulish Mehdi (1 replies)
Related podcasts
-
ADO.NET Data Services in .NET 3.5 Service Pack 1 Beta1 with ASP.NET AJAX
Wally walks through using ASP.NET Podcast Show #114 - ADO.NET Data Services in .NET 3.5 Service Pack 1 Beta1 with ASP.NET AJAX.
Events coming up
-
Nov
18
15 Minutes of Fame
Dresher, United States
This is a yearly tradition. We select 10 of the favorite speakers from monthly meetings, code camps, and hands on labs. Each one does a 15 minute talk on their favorite .NET technology. This is our 10th anniversary so we plan a gala event with special prizes and refreshments.
An obscure side issue -
I was using this technique with SQL Server with no problems but my app had to access Oracle as well. That gave me two problems; the OracleClient would not accept a ';' in the command string. This also meant that I did not find out how to make it accept multiple requests in one SQL statement.
It might save some-one a day's struggle knowing that ('
This thread is for discussions of How to run through multiple DataReader results.