Is there any gain in reading the fields asynchronously?
Say if I have the following:
SqlDataReader reader = await cmd.ExecuteReaderAsync(); while (await reader.ReadAsync()) { try { if (reader.IsDBNull(COL_NAME)) { continue; } user = new User(); user.Id = reader.GetInt32(COL_ID);
Would there be any real world benefit by using IsDBNullAsync
and GetInt32Async
etc?
As explained earlier, the SqlDataReader returns data via a sequential stream. To read this data, you must pull data from a table row-by-row Once a row has been read, the previous row is no longer available.
It is used to populate an array of objects with the column values of the current row. It is used to get the next result, when reading the results of SQL statements. It is used to read record from the SQL Server database. To create a SqlDataReader instance, we must call the ExecuteReader method of the SqlCommand object.
While a DataReader is open, you can retrieve schema information about the current result set using the GetSchemaTable method. GetSchemaTable returns a DataTable object populated with rows and columns that contain the schema information for the current result set.
After creating an instance of the Command object, you create a DataReader by calling Command. ExecuteReader to retrieve rows from a data source, as shown in the following example. SqlDataReader myReader = myCommand. ExecuteReader();
After some peeking at reflector, the interesting methods here (GetFieldValueAsync<T>
, IsDBNullAsync
, and the internal
method GetBytesAsync
) only do "interesting" code for the CommandBehavior.SequentialAccess
scenario. So: if you're not using that: don't bother - the row data is already buffered in memory, and Task<T>
is pure overhead (although it will at least be an already-completed task result, i.e. Task.FromResult<T>
- which is handled efficiently by await
, without a context switch).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With