Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 25,395 times

Related Categories

How to run through multiple DataReader results

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();

Edward Tanguay updates his personal web site tanguay.info weekly with code, links, quotes and thoughts on web development. Sign up for the free newsletter.

Comments

  • Multiple DataReader Results (on Oracle)

    Posted by jtevans on 02 Mar 2005

    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 t...