Articles → ADO.NET → Reading Multiple Result Sets Using Datareader In Ado.Net

Reading Multiple Result Sets Using Datareader In Ado.Net






Syntax


//  This will read the first result set
while ( <data_reader_object> .Read()) {
  //  Do something
}
//  Advances to next result set
<data_reader_object> .NextResult();
//  This will read the second result set
while ( <data_reader_object> .Read()) {
  //  Do something
}
//  Advances to next result set
<data_reader_object> .NextResult();
//  This will read the third result set
while ( <data_reader_object> .Read()) {
  //  Do something
}







Example


using(SqlConnection conn = new SqlConnection("<connection_string>")) {
  using(SqlCommand cmd = new SqlCommand("query1;query2;query3", conn)) {
    conn.Open();
    SqlDataReader rd = cmd.ExecuteReader();
    //  This will read the first result set
    while (rd.Read()) {
      //  Do something 
    }
    //  Advances to next result set
    rd.NextResult();
    //  This will read the second result set
    while (rd.Read()) {
      //  Do something 
    }
    //  Advances to next result set
    rd.NextResult();
    //  This will read the second result set
    while (rd.Read()) {
      //  Do something 
    }
  }
}



Posted By  -  Karan Gupta
 
Posted On  -  Saturday, December 10, 2011

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250