Michael M.
Michael M.

Reputation: 283

Are there any known issues with mixing async and non async calls on the same SqlDataReader?

Say for example I open a connection async. I execute my reader async. I begin using my reader as normal:

if (await reader.NextResultAsync())
            {
                while (await reader.ReadAsync())
                {
                   //work
                }
              }

I then on the same reader do a non async version of this:

if (reader.NextResult())
            {
                while (reader.Read())
                {
                    //Work
                 }
             }

Are there any known issues this can cause? I am reviewing some legacy code and this is obviously inconsistent; but is it going to cause any issues at run time mixing them like this?

Thanks,

Upvotes: 2

Views: 52

Answers (1)

usr
usr

Reputation: 171178

This does not cause any issues.

In fact this is not always a bad idea. Async calls have more overhead than sync calls. It is reasonable to obtain a reader and result set asynchronously (since that might take a long time) and then synchronously reads rows (since each row is likely to be available quickly).

Upvotes: 1

Related Questions