IDataReader/DataReader With Multiple Result Sets

Posted: September 28, 2011 in ASP.Net

When using IDataReader/DataReader with multiple result sets, you can go through each of the result sets using the NextResult() command. For the example below I am assuming that the stored procedure is returning 3 result sets:

try
{
  Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase DAL = new Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase(MyConnectionString);
  System.Data.Common.DbCommand dbgCommand = DAL.GetStoredProcCommand("MyStoredProcedure");
  DAL.AddInParameter(dbgCommand, "@MyParameter", System.Data.SqlDbType.Int, MyIntValue);
  IDataReader rdr = DAL.ExecuteReader(dbgCommand);
  try
  {
    while (rdr.Read())
    {
      // Result set 1
    }
    
    rdr.NextResult();
    while (rdr.Read())
    {
      // Result set 2
    }

    rdr.NextResult();
    while (rdr.Read())
    {
      // Result set 3
    }
  }
  finally
  {
    rdr.Close();
    rdr.Dispose();
  }
 }
catch (Exception ex)
{
  ...
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s