SqlDataAdapter.Fill asynchronously (no FillAsync in ADO.NET)
Codemia Knowledge Hub ·
SqlDataAdapter has no native FillAsync() method. To fill a DataTable without blocking a thread, run the query with SqlCommand.ExecuteReaderAsync() and load the reader into the table with DataTable.Load(), or read the rows yourself with ReadAsync().
Why synchronous Fill is a problem
In an ASP.NET request handler this blocks the thread and reduces scalability. On a WPF UI thread it freezes the UI.
ExecuteReaderAsync plus DataTable.Load (recommended)
DataTable.Load() itself is synchronous, but opening the connection and executing the query are awaited through OpenAsync() and ExecuteReaderAsync().
Loading rows one by one with ReadAsync
This first variant still loads through DataTable.Load(), so it is not actually row by row:
For fully async reading, build the columns from the reader and add each row after ReadAsync():
Task.Run wrapper (avoid in ASP.NET)
Wrapping the synchronous call in Task.Run moves it to a thread pool thread but does not give true async I/O:
This is acceptable in WPF or WinForms to unblock the UI thread, but wasteful in ASP.NET, where thread pool threads are precious.
A reusable FillAsync extension method
Passing parameters
Dapper as an alternative
Dapper is fully async and maps results directly to objects, which removes the need for a DataTable in most cases.
Things to get right
- Exceptions: wrap async calls in try/catch where needed, especially for
SqlException. - Disposal: use
usingorawait usingfor everyIDisposableto avoid leaks and connection pool exhaustion. - Concurrency and pooling: configure the client for an appropriate number of concurrent connections and manage connection pooling.
- Task.Run in ASP.NET: wrapping
Fill()wastes a thread pool thread on blocking I/O. UseExecuteReaderAsyncinstead. - DataTable thread safety:
DataTableis not thread-safe. Do not share one across async operations without synchronization. - Connection lifetime: open with
OpenAsync, read the data, then letusingdispose the connection.
Related reading
- SqlDateTime.MinValue DateTime.MinValue, why?
- sqlite3.ProgrammingError Incorrect number of bindings supplied. The current statement uses 1, and there are 74 supplied
- SQLite - ORDER BY RAND
- SQLite equivalent to ISNULL, NVL, IFNULL or COALESCE
- SQLite Sharing Connections across threads to read and write
- SslStream asynchronous methods always return an IAsyncResult where CompletedSynchronously is True. Why?
- SqlParameterCollection only accepts non-null SqlParameter type objects, not String objects
- Stack capacity in C
System design interview problems on this topic
- Design a Distributed OLTP Database: solution and walkthrough
- Design a Wide Column Database: solution and walkthrough
- Design a Key Value Store: solution and walkthrough
- Design a Web Cache: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.