Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any benefits of reading each field async from a SqlDataReader?

Tags:

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?

like image 803
Carl R Avatar asked Nov 10 '13 20:11

Carl R


People also ask

What does SqlDataReader return?

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.

Why do we use SqlDataReader?

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.

Which method of SQL command class returns resultset as a DataReader 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.

How to add SqlDataReader in c#?

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();


1 Answers

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).

like image 106
Marc Gravell Avatar answered Sep 28 '22 11:09

Marc Gravell