How can I load a CSV file into a System.Data.DataTable
, creating the datatable based on the CSV file?
Does the regular ADO.net functionality allow this?
I've recently written a CSV parser for .NET that I'm claiming is currently the fastest available as a nuget package: Sylvan.Data.Csv.
Using this library to load a DataTable
is extremely easy.
using var tr = File.OpenText("data.csv");
using var dr = CsvDataReader.Create(tr);
var dt = new DataTable();
dt.Load(dr);
Assuming your file is a standard comma separated files with headers, that's all you need. There are also options to allow reading files without headers, and using alternate delimiters etc.
It is also possible to provide a custom schema for the CSV file so that columns can be treated as something other than string
values. This will allow the DataTable
columns to be loaded with values that can be easier to work with, as you won't have to coerce them when you access them.
var schema = new TypedCsvSchema();
schema.Add(0, typeof(int));
schema.Add(1, typeof(string));
schema.Add(2, typeof(double?));
schema.Add(3, typeof(DateTime));
schema.Add(4, typeof(DateTime?));
var options = new CsvDataReaderOptions {
Schema = schema
};
using var tr = GetData();
using var dr = CsvDataReader.Create(tr, options);
TypedCsvSchema
is an implementation of ICsvSchemaProvider which provides a simple way to define the types of the columns. However, it is also possible to provide a custom ICsvSchemaProvider
when you want to provide more metadata, such as uniqueness or constrained column size, etc.