Retrieving Multiple Record Sets Through a DataReader

If a stored procedure returns multiple record sets, and if you are using the DataReader to retrieve the results, you can use the NextResult method of the DataReader to iterate through the Record Sets. Sample shown below:

SqlConnection conn = new SqlConnection("connectionStringValue")); conn.Open(); using (conn) {     using (SqlCommand command = new SqlCommand(" SELECT col1 FROM Table1; SELECT col2 FROM Table2",    conn))    {      using (SqlDataReader reader = command.ExecuteReader())      {          while (reader.HasRows)          { while (reader.Read())           { // }        reader.NextResult();           }        }  }} 
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: