Async Programming
SqlDataAdapter
C# ADO.NET
Database Operations
.NET Development

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

csharp
1using System.Data;
2using System.Data.SqlClient;
3
4// This blocks the calling thread until the query completes
5var adapter = new SqlDataAdapter("SELECT * FROM Orders", connectionString);
6var dataTable = new DataTable();
7adapter.Fill(dataTable);  // synchronous — no async version exists

In an ASP.NET request handler this blocks the thread and reduces scalability. On a WPF UI thread it freezes the UI.

csharp
1public static async Task<DataTable> FillAsync(string query, string connectionString)
2{
3    var dataTable = new DataTable();
4
5    using var connection = new SqlConnection(connectionString);
6    using var command = new SqlCommand(query, connection);
7
8    await connection.OpenAsync();
9    using var reader = await command.ExecuteReaderAsync();
10    dataTable.Load(reader);
11
12    return dataTable;
13}
14
15// Usage
16var orders = await FillAsync("SELECT * FROM Orders", connectionString);

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:

csharp
1public static async Task<DataTable> FillAsyncComplete(string query, string connectionString)
2{
3    var dataTable = new DataTable();
4
5    using var connection = new SqlConnection(connectionString);
6    using var command = new SqlCommand(query, connection);
7
8    await connection.OpenAsync();
9    using var reader = await command.ExecuteReaderAsync();
10
11    // Load builds the schema and reads every row synchronously
12    dataTable.Load(reader);
13    // Alternative: build schema manually from reader.GetSchemaTable()
14
15    return dataTable;
16}

For fully async reading, build the columns from the reader and add each row after ReadAsync():

csharp
1public static async Task<DataTable> FillAsyncManual(string query, string connectionString)
2{
3    var dataTable = new DataTable();
4
5    await using var connection = new SqlConnection(connectionString);
6    await using var command = new SqlCommand(query, connection);
7
8    await connection.OpenAsync();
9    await using var reader = await command.ExecuteReaderAsync();
10
11    // Build columns from schema
12    for (int i = 0; i < reader.FieldCount; i++)
13    {
14        dataTable.Columns.Add(reader.GetName(i), reader.GetFieldType(i));
15    }
16
17    // Read rows asynchronously
18    while (await reader.ReadAsync())
19    {
20        var row = dataTable.NewRow();
21        for (int i = 0; i < reader.FieldCount; i++)
22        {
23            row[i] = await reader.IsDBNullAsync(i) ? DBNull.Value : reader.GetValue(i);
24        }
25        dataTable.Rows.Add(row);
26    }
27
28    return dataTable;
29}

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:

csharp
1// Not recommended — uses a thread pool thread for blocking I/O
2var dataTable = await Task.Run(() =>
3{
4    var adapter = new SqlDataAdapter("SELECT * FROM Orders", connectionString);
5    var dt = new DataTable();
6    adapter.Fill(dt);
7    return dt;
8});

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

csharp
1public static class SqlDataAdapterExtensions
2{
3    public static async Task<int> FillAsync(this SqlDataAdapter adapter, DataTable dataTable)
4    {
5        using var reader = await adapter.SelectCommand.ExecuteReaderAsync();
6        dataTable.Load(reader);
7        return dataTable.Rows.Count;
8    }
9}
10
11// Usage
12var adapter = new SqlDataAdapter("SELECT * FROM Orders", connectionString);
13adapter.SelectCommand.Connection = new SqlConnection(connectionString);
14await adapter.SelectCommand.Connection.OpenAsync();
15
16var dataTable = new DataTable();
17await adapter.FillAsync(dataTable);

Passing parameters

csharp
1public static async Task<DataTable> QueryAsync(
2    string query,
3    string connectionString,
4    params SqlParameter[] parameters)
5{
6    var dataTable = new DataTable();
7
8    await using var connection = new SqlConnection(connectionString);
9    await using var command = new SqlCommand(query, connection);
10
11    command.Parameters.AddRange(parameters);
12
13    await connection.OpenAsync();
14    await using var reader = await command.ExecuteReaderAsync();
15    dataTable.Load(reader);
16
17    return dataTable;
18}
19
20// Usage
21var orders = await QueryAsync(
22    "SELECT * FROM Orders WHERE CustomerId = @id",
23    connectionString,
24    new SqlParameter("@id", customerId)
25);

Dapper as an alternative

csharp
1using Dapper;
2
3await using var connection = new SqlConnection(connectionString);
4var orders = (await connection.QueryAsync<Order>(
5    "SELECT * FROM Orders WHERE CustomerId = @Id",
6    new { Id = customerId }
7)).ToList();

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 using or await using for every IDisposable to 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. Use ExecuteReaderAsync instead.
  • DataTable thread safety: DataTable is not thread-safe. Do not share one across async operations without synchronization.
  • Connection lifetime: open with OpenAsync, read the data, then let using dispose the connection.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design