How to read SQL Table data into a C DataTable
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Reading SQL table data into a C# DataTable is a standard ADO.NET task when you want a disconnected, in-memory representation of tabular data. The most common approach uses SqlDataAdapter.Fill, though SqlDataReader plus DataTable.Load is also valid. The right choice depends on how much control you want over execution and whether you need only a quick fill or a more streaming-style read path first.
Use SqlDataAdapter.Fill
For many applications, SqlDataAdapter is the simplest answer. You provide a connection, a query, and an empty DataTable, then call Fill.
This is the usual starting point because it is compact and handles the connection lifecycle cleanly through using.
Use Parameters for Filtered Queries
If the query includes user-controlled values, use parameters instead of concatenating SQL strings.
This avoids SQL injection risks and also handles proper quoting and typing for the database provider.
SqlDataReader Plus DataTable.Load
If you prefer to execute the command yourself and then load the result into a DataTable, use a reader.
This version is useful when you want more explicit control over the command execution path before the data lands in the table.
Why Use a DataTable at All
A DataTable is most useful when you want:
- disconnected access after the query finishes
- flexible row and column inspection
- binding to older UI components or reporting tools
If the application really wants typed domain objects, direct mapping to C# classes may be a better fit. But for quick in-memory tabular work, DataTable is still practical.
Handle Nulls and Types Carefully
Database columns may contain NULL, and DataTable stores values as object. That means code reading values should check for DBNull.Value.
Assuming every field is present and strongly typed is a common source of runtime errors.
Keep the Query Narrow
When filling a DataTable, it is tempting to select everything. In practice, selecting only the needed columns reduces memory use and makes downstream code clearer.
This matters especially when the table is large or the query is used in high-traffic code paths. SELECT * is convenient early on but usually a poor long-term habit.
Common Pitfalls
- Concatenating SQL strings instead of using parameters.
- Loading an entire table when only a few columns or rows are needed.
- Forgetting that
DataTablevalues are object-based and may containDBNull.Value. - Skipping
usingblocks and leaving connections or commands open longer than necessary. - Using
DataTableautomatically even when typed models would fit the code better.
Summary
- '
SqlDataAdapter.Fillis the simplest common way to load SQL data into aDataTable.' - '
SqlDataReaderplusDataTable.Loadis a good alternative when you want more explicit control.' - Use parameters for filtered queries.
- Check for
DBNull.Valuewhen reading nullable columns. - '
DataTableis best when you want disconnected, flexible tabular access rather than strongly typed domain objects.'
Related reading
- how to rebalance cassandra cluster after adding new node
- How to recover MySQL database from .myd, .myi, .frm files
- How to reduce storage scale down my AWS RDS instance?
- How to remove all MySQL tables from the command-line without DROP database permissions?
- How to recursively list all the files in a directory in C?
- How to redirect to a dynamic login URL in ASP.NET MVC
- How to remove an element from a doubly-nested array in a MongoDB document
- How to remove constraints from my MySQL table?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.