DataReader vs DataAdapter
Intermediate
20 min read
Lesson 4 of 5
SqlDataReader — forward-only streaming
Fast and lightweight. Reads one row at a time and doesn't hold the whole result set in memory. Best for simply looping through and printing/using data once.
using var reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader["FirstName"]);
}
SqlDataAdapter + DataTable — an in-memory snapshot
Loads the entire result into a DataTable you can pass around,
bind to a grid (very common in Windows Forms), filter, or sort without
touching the database again.
using var connection = new SqlConnection(connectionString);
var adapter = new SqlDataAdapter("SELECT * FROM Students", connection);
var table = new DataTable();
adapter.Fill(table);
foreach (DataRow row in table.Rows)
{
Console.WriteLine(row["FirstName"]);
}
Which one should you use?
| Scenario | Use |
|---|---|
| Web API / Razor Pages, read once and render | SqlDataReader |
| Windows Forms DataGridView binding | SqlDataAdapter + DataTable |
| Need to edit data offline then push changes back | SqlDataAdapter (supports Update()) |
Key Takeaway
Use SqlDataReader for fast forward-only reading. Use SqlDataAdapter + DataTable for binding to grids or editing data offline.