Retrieving Multiple Record Sets Through a DataReader

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

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist