C# Multiple Result Sets
The DataReader Object in C# provides a stream-based, forward-only, read-only retrieval of query results from the Data Source without the ability to update the data.
ExecuteReader() method
When the ExecuteReader() method is invoked on the SqlCommand Object, it instantiates a SqlClient.SqlDataReader Object in C#. This SqlDataReader Object allows you to efficiently read and access the retrieved data from the Data Source.
In certain scenarios, there may be a need to execute multiple SQL statements using the same Command Object. In such cases, the SqlDataReader can handle multiple ResultSets. Each ResultSet represents the result of an individual SQL statement within the batch executed.
NextResult() method
To retrieve multiple ResultSets from the SqlDataReader, you can use the NextResult() method. After reading the first ResultSet, invoking the NextResult() method advances the SqlDataReader to the next ResultSet, if available. This allows you to sequentially access the data from each ResultSet within the SqlDataReader.
By utilizing the NextResult() method iteratively, you can navigate through all the ResultSets returned by the SqlDataReader and retrieve the corresponding data as needed.
In the following source code you can see how to get multiple result sets from SqlDataReader() .
Full Source C#Conclusion
The DataReader Object in C# provides a stream-based, forward-only, read-only retrieval of query results from the Data Source. The SqlDataReader instantiated by the ExecuteReader() method in the SqlCommand Object facilitates this data retrieval. In situations where multiple SQL statements are executed using the Command Object, the SqlDataReader can handle multiple ResultSets. The NextResult() method allows you to navigate between these ResultSets and retrieve data from each one.
- C# ADO.NET Command
- C# ADO.NET SqlCommand - ExecuteNonQuery
- C# ADO.NET OleDbCommand - ExecuteNonQuery
- C# ADO.NET SqlCommand - ExecuteScalar
- C# ADO.NET OleDbCommand - ExecuteScalar
- C# ADO.NET SqlCommand - ExecuteReader
- C# ADO.NET OleDbCommand - ExecuteReader
- C# ADO.NET DataReader
- C# ADO.NET SqlDataReader
- C# ADO.NET OleDbDataReader
- C# Table Schema from SqlDataReader
- C# Table Schema from OleDbDataReader
- C# ADO.NET DataAdapter
- C# ADO.NET SqlDataAdapter
- C# ADO.NET OleDbDataAdapter
- C# ExecuteReader and ExecuteNonQuery
- System.Data.SqlClient.SqlException: Login failed for user