Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between SqlDataReader.Read and SqlDataReader.NextResult

Tags:

c#

sql

ado.net

What is the main difference between these two methods? On the msdn website it is explained like below but I don't understand it.

Read Advances the SqlDataReader to the next record. (Overrides DbDataReader.Read().)

NextResult Advances the data reader to the next result, when reading the results of batch Transact-SQL statements. (Overrides dbDataReader.NextResult().)

like image 438
Ozkan Avatar asked Nov 20 '14 16:11

Ozkan


People also ask

What is NextResult in C#?

NextResult Advances the data reader to the next result, when reading the results of batch Transact-SQL statements. (Overrides dbDataReader. NextResult().) c# sql ado.net.

What is SqlDataReader explain it with relevant example?

The SqlDataReader is used to read a row of record at a time which is got using SqlCommand. It is read only, which means we can only read the record; it can not be edited. And also it is forward only, which means you can not go back to a previous row (record).

What is SqlDataReader in Ado net?

ADO.NET SqlDataReader Class. This class is used to read data from SQL Server database. It reads data in forward-only stream of rows from a SQL Server database. it is sealed class so that cannot be inherited.

What is the use of SqlDataReader in asp net?

The ADO.NET SqlDataReader class in C# is used to read data from the SQL Server database in the most efficient manner. It reads data in the forward-only direction. It means, once it read a record, it will then read the next record, there is no way to go back and read the previous record. SqlDataReader is read-only.


1 Answers

If your statement/proc is returning multiple result sets, For example, if you have two select statements in single Command object, then you will get back two result sets.

  • NextResult is used to move between result sets.
  • Read is used to move forward in records of a single result set.

Consider the following example:

If you have a proc whose main body is like:

.... Proc start  SELECT Name,Address FROM Table1  SELECT ID,Department FROM Table2  -- Proc End 

Executing the above proc would produce two result sets. One for Table1 or first select statement and other for the next select statement.

By default first result set would be available for Read. If you want to move to second result set, you will need NextResult.

See: Retrieving Data Using a DataReader

Example Code from the same link: Retrieving Multiple Result Sets using NextResult

static void RetrieveMultipleResults(SqlConnection connection) {     using (connection)     {         SqlCommand command = new SqlCommand(           "SELECT CategoryID, CategoryName FROM dbo.Categories;" +           "SELECT EmployeeID, LastName FROM dbo.Employees",           connection);         connection.Open();          SqlDataReader reader = command.ExecuteReader();          while (reader.HasRows)         {             Console.WriteLine("\t{0}\t{1}", reader.GetName(0),                 reader.GetName(1));              while (reader.Read())             {                 Console.WriteLine("\t{0}\t{1}", reader.GetInt32(0),                     reader.GetString(1));             }             reader.NextResult();         }     } } 
like image 122
Habib Avatar answered Sep 28 '22 01:09

Habib