Is the .NET SqlDataReader using the database cursor, or is the entire result set loaded into RAM?

Here's a sample usage code SqlDataReader

:

// Working with SQLServer and C#
// Retrieve all rows
cmd.CommandText = "SELECT some_field FROM data";

using (var reader = cmd.ExecuteReader())
{
    while (reader.Read())
    {
        Console.WriteLine(reader.GetString(0));
    }
}

      

EDIT:

What I want to say is that I want to understand if the same mechanism exists when retrieving data from the database in a while loop (in case SqlDataReader

) as it does when working with SQLite.

// working with SQLite and Java
if (cursor.moveToFirst()) {
   do {
      String data = cursor.getString(cursor.getColumnIndex("data"));
      // do what ever you want here
   } while(cursor.moveToNext());
}
cursor.close();

      

+3


source to share


1 answer


No, there is no server side cursor unless your command is calling a stored procedure that uses the cursor. The server returns an equal-vanilla SQL result set, one row at a time, when you use the SqlDataReader. Obviously, the data must be somewhere before you can read it, and that place will be the buffer (s) that SQL Server and drivers manage.



If you were to use this using something like a dataset, then all the rows would be in RAM at once.

+3


source







All Articles