How to read a CSV file into a .NET Datatable
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Reading a CSV file into a DataTable in .NET can be done with the built-in TextFieldParser class, manual StreamReader parsing, or the popular CsvHelper NuGet package. TextFieldParser handles quoted fields and delimiters correctly out of the box. For production code with complex CSV files (embedded commas, multiline fields, different encodings), CsvHelper is the most reliable option.
Using TextFieldParser (Built-in)
TextFieldParser is part of Microsoft.VisualBasic.FileIO and works in any .NET project:
Add the Microsoft.VisualBasic reference if it is not already included. In .NET Core/.NET 5+, add the NuGet package Microsoft.VisualBasic.
Using StreamReader (Manual Parsing)
For simple CSV files without quoted fields:
This approach breaks on fields containing commas, newlines, or quotes. Use TextFieldParser or CsvHelper for robust parsing.
Using CsvHelper (NuGet Package)
Install the package:
Read CSV into a DataTable:
CsvDataReader adapts CsvHelper to the IDataReader interface, which DataTable.Load() consumes directly.
Custom Configuration
Typed Column DataTable
By default, all columns are strings. To set specific data types:
Handling Large Files
For CSV files that are too large to load entirely into memory:
Writing a DataTable Back to CSV
Common Pitfalls
- Splitting on commas with
String.Split: Fields containing commas (e.g.,"New York, NY") break manualSplit(',')parsing. UseTextFieldParserorCsvHelperwhich handle quoted fields correctly. - Encoding issues: CSV files from Excel are often in Windows-1252 encoding, not UTF-8. If you see garbled characters, specify the encoding:
new StreamReader(filePath, Encoding.GetEncoding(1252)). - Empty rows and trailing newlines: Many CSV files end with a blank line. Check for empty rows before adding them to the DataTable:
if (fields.All(f => string.IsNullOrEmpty(f))) continue;. - Column count mismatch: If a row has fewer fields than the header,
dataTable.Rows.Add(fields)throws an exception. Check the field count before adding, or useCsvHelperwithMissingFieldFound = nullto handle this gracefully. - Date and number locale:
decimal.Parse("1.234,56")fails in US locale. UseCultureInfo.InvariantCultureor the specific culture matching your CSV data format.
Summary
- Use
TextFieldParserfor a built-in solution that handles quoted fields correctly - Use
CsvHelper(NuGet) for production code with complex CSV formats - Manual
StreamReader+Splitonly works for simple, well-formed CSV files - Set
HasFieldsEnclosedInQuotes = trueto handle quoted fields with embedded commas - Process large files in batches to avoid memory issues
- Always specify encoding when reading CSV files from external sources
Related reading
- How to read a PEM RSA private key from .NET
- How to read a text file reversely with iterator in C
- How to read action method's attributes in ASP.NET Core MVC?
- How to read assembly attributes
- How to read attribute value from XmlNode in C?
- How to read data from excel file using c
- How to read file using NPOI
- How to read SQL Table data into a C DataTable

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.