title: Should I upgrade to be able to use the new async methods for SqlDataReader?
body: <p>I'm working on a large project that runs on .NET 4.0. This framework uses ADO.NET for database calls and we're currently adding asynchronous API methods. The <code>SqlCommand</code> class has the <a href="http://msdn.microsoft.com/en-us/library/ms228963%28v=vs.110%29.aspx" rel="noreferrer">APM</a> methods <code>SqlCommand.BeginExecuteReader()</code> and <code>SqlCommand.EndExecuteReader()</code>, but <code>SqlDataReader</code> does not have asynchronous implementations.<br>
  When the <code>SqlCommand.ExecuteReader()</code> is finished I want to iterate through the results using <code>SqlDataReader</code>. Microsoft introduces asynchronous methods for <code>SqlDataReader</code> in .NET 4.5, so I can't use those in 4.0. </p>
  
  <p><strong>Question</strong>: Should we upgrade to be able to use the asynchronous (<a href="http://msdn.microsoft.com/en-us/library/hh873175%28v=vs.110%29.aspx" rel="noreferrer">TAP</a>) methods of <code>SqlDataReader</code>?
  If we do, why?</p>
  
  <blockquote>
    <p>I searched the web and stackoverflow alot for answers, but I only seem to find implementations for this. It doesn't tell me what benefit those new implementations give.</p>
  </blockquote>
  
  <h2>.NET 4.0 implementation</h2>
  
  <p>Here we use the asynchronous methods of <code>SqlCommand</code>, but we can't use the new asynchronous methods for <code>SqlDataReader</code>, like <code>SqlDataReader.ReadAsync()</code>.
  </p>
  
  <pre class="lang-c# prettyprint-override"><code>private Task&lt;IDataReader&gt; ExecuteReaderAsync(IDbCommand dbCommand)
  {
      var sqlCommand = CheckIfSqlCommand(dbCommand);
      PrepareExecuteReader(dbCommand);
  
      return Task&lt;IDataReader&gt;
          .Factory
          .FromAsync(sqlCommand.BeginExecuteReader, sqlCommand.EndExecuteReader, null);
  }
  
  private void ReadAll(Task&lt;IDataReader&gt; readerTask)
  {
      var reader = readerTask.Result;
  
      while (reader.Read()) // Should this be asynchronously?
      {
          // Do something
      }
  }
  
  public Task&lt;IDataReader&gt; Foo(IDbCommand dbCommand) {
      return ExecuteReaderAsync(dbCommand)
          .ContinueWith(readerTask =&gt; ReadAll(readerTask));
  }
  </code></pre>
  
  <h2>.NET 4.5 implementation</h2>
  
  <p>In .NET 4.5 we can use the async/await keywords, and we can use the new asynchronous methods for <code>SqlDataReader</code>, like <code>SqlDataReader.ReadAsync()</code>.
  </p>
  
  <pre class="lang-c# prettyprint-override"><code>private async Task&lt;SqlDataReader&gt; ExecuteReaderAsync(SqlCommand dbCommand)
  {
      PrepareExecuteReader(dbCommand);
  
      return await dbCommand.ExecuteReaderAsync();
  }
  
  private async Task ReadAll(SqlDataReader reader)
  {
      while (await reader.ReadAsync()) // Should this be asynchronously?
      {
          // Do something
      }
  }
  
  public async Task&lt;IDataReader&gt; Foo(SqlCommand dbCommand)
  {
      var reader = await ExecuteReaderAsync(dbCommand);
      await ReadAll(reader);
  
      return reader;
  }
  </code></pre>