🔄 How to Read Multiple Result Sets Using ADO.NET in C#

Published At 2025/Jul/10
No Image Found

📌 Introduction

When querying a database, especially in complex applications, it's common to return multiple result sets in a single SQL query or stored procedure. Instead of making multiple database calls, you can boost performance and reduce overhead by fetching all the data at once.

In this post, we'll learn how to read multiple result sets using ADO.NET with SqlDataReader.

 

📄 Scenario

Suppose you want to display:

  • A list of products

  • Each product's average review rating

  • Related tags

Instead of calling three separate queries, we’ll combine the result into one and parse them sequentially using SqlDataReader.

💻 Code Example

How to Read Multiple Result Sets Using ADO.NET

Introduction

When querying a database, especially in complex applications, it’s common to return multiple result sets in a single SQL query or stored procedure. Instead of making multiple database calls, you can boost performance and reduce overhead by fetching all the data at once.

In this guide, we’ll learn how to read multiple result sets using ADO.NET with SqlDataReader.


Scenario

Suppose you want to display:

  • A list of products
  • Each product’s average review rating
  • Related tags

Instead of calling three separate queries, we’ll combine the result into one and parse them sequentially using SqlDataReader.


Code Example

  1. using (SqlConnection conn = new SqlConnection(connectionString))
  2. using (SqlCommand cmd = new SqlCommand("your-multi- select-stored-procedure-or-query", conn))
  3. {
  4. conn.Open();
  5. using (SqlDataReader reader = cmd.ExecuteReader())
  6. {
  7. // 1st result set: Products
  8. List<Product> products = new List<Product>();
  9. while (reader.Read())
  10. {
  11. products.Add(new Product {
  12. Id = reader.GetInt32(0),
  13. Title = reader.GetString(1),
  14. // Add other columns as needed
  15. });
  16. }
  17. // 2nd result set: Reviews
  18. if (reader.NextResult())
  19. {
  20. List<Review> reviews = new List<Review>();
  21. while (reader.Read())
  22. {
  23. reviews.Add(new Review {
  24. ProductId = reader.GetInt32(0),
  25. AvgStar = reader.GetDecimal(1)
  26. });
  27. }
  28. }
  29. // 3rd result set: Tags
  30. if (reader.NextResult())
  31. {
  32. List<Tag> tags = new List<Tag>();
  33. while (reader.Read())
  34. {
  35. tags.Add(new Tag {
  36. Id = reader.GetInt32(0),
  37. Title = reader.GetString(1)
  38. });
  39. }
  40. }
  41. // Add more reader.NextResult() as needed
  42. }
  43. }

Handling Multiple SELECT Queries in SQL with ADO.NET

Example SQL Query

You can write a query like this in your stored procedure or script:

  1. -- Multiple SELECTs
  2. ```sql
  3. SELECT Id, Title FROM Products;
  4. SELECT ProductId, AVG(Stars) AS AvgStar FROM Reviews GROUP BY ProductId;
  5. SELECT TagId, Title FROM Tags;

Notes

  • Always check reader.NextResult() before reading the next result set.
  • This technique is especially useful for dashboard-style pages that need multiple sets of data.
  • Use models (Product, Review, Tag) to store each dataset logically.

Benefits

  • Fewer database calls = better performance
  • Cleaner code with centralized query logic
  • Faster UI loading for pages needing multiple datasets

Conclusion

Using SqlDataReader.NextResult() allows you to handle multiple datasets efficiently within a single database connection. It’s a powerful tool when working with dashboards, reports, or other data-heavy applications.

Want to reduce server round-trips? Master SqlDataReader with multi-select handling!