I've noticed that many people don't understand how queries are performed to retrieve data from a database.
Often there is a code like the following:
var reader = await sqlCommand.ExecuteReaderAsync();
dataTable.Load(reader);
There are several answers on StackOverflow, which present it as an asynchronous download of the data to the DataTable
.
However, I believe that the ExecuteReader
method does not load data, but only creates an instance of the reader.
I can confirm this with a quote from official documentation:
The DataReader provides an unbuffered stream of data that allows procedural logic to efficiently process results from a data source sequentially. The DataReader is a good choice when you're retrieving large amounts of data because the data is not cached in memory.
One person in a dispute with me cited a link and claimed that the data download was taking place there. But I don't see a call to the Read method inside this code! Meanwhile, the ExecuteScalarAsync
method just below has such a call.
Hence the data will be loaded with the DataTable.Load
method or will be done with the while(reader.Read())
loop. Am I right?
So where are the data loaded: in the ExecuteReader(Async)
method or in the Read/Load
method?