Roadmap › ADO.NET — Connected & Disconnected Data Access › Module: Disconnected Architecture
Lessons in this course (9)
5. DataSet, DataTable and DataAdapter
IntermediateMediumLesson 5 of 9
csharp
var ds = new DataSet();
using var da = new SqlDataAdapter("SELECT Id, Name, Price FROM Products", cs);
da.Fill(ds, "Products"); // opens + closes connection automatically
DataTable t = ds.Tables["Products"]!;
foreach (DataRow row in t.Rows) Console.WriteLine($"{row["Name"]}: {row["Price"]}");
t.Rows[0]["Price"] = 999m; // RowState = Modified
var cb = new SqlCommandBuilder(da); // auto-generates INSERT/UPDATE/DELETE (needs a primary key)
da.Update(ds, "Products"); // pushes changes back
DataView view = new(t) { RowFilter = "Price > 1000", Sort = "Price DESC" };| DataReader | DataSet |
|---|---|
| Connected, forward-only, read-only | Disconnected, in-memory, random access, editable |
| Very fast, low memory | Heavier; holds all rows |
| One row at a time | Multiple tables + relations + constraints |
| Best for APIs, reports streaming | Legacy WinForms grids, offline editing, XML serialisation |
Modern apps rarely use DataSets; they map to POCOs with Dapper/EF.