CsvConverterFill(DataTable, String, CsvMapping, Char, Encoding, Boolean, CsvOpts) Method

Adds the content of a CSV file as DataRows to a DataTable.

Definition

Namespace: FolkerKinzel.CsvTools.Mappings
Assembly: FolkerKinzel.CsvTools.Mappings (in FolkerKinzel.CsvTools.Mappings.dll) Version: 1.1.0+1263e8243dc2cd78095f678f813d7d9c52ea4315
C#
public static void Fill(
	DataTable dataTable,
	string filePath,
	CsvMapping mapping,
	char delimiter = ',',
	Encoding? textEncoding = null,
	bool isHeaderPresent = true,
	CsvOpts options = CsvOpts.Default
)

Parameters

dataTable  DataTable
The DataTable to which DataRows are added.
filePath  String
File path of the CSV file.
mapping  CsvMapping
The CsvMapping to be used.
delimiter  Char  (Optional)
The field separator character.
textEncoding  Encoding  (Optional)
The text encoding to be used to read the CSV file or null for UTF8.
isHeaderPresent  Boolean  (Optional)
true, to interpret the first line as a header, otherwise false.
options  CsvOpts  (Optional)
Options for reading the CSV file.

Remarks

Each PropertyName of mapping MUST have a corresponding DataColumn in dataTable - corresponding in the Caption property (case-insensitive) and the accepted data type.

Effort must be taken that the PropertyNames in mapping are unique, even when treated case-insensitive.

The DynamicProperty instances in mapping don't need to match all columns of the DataTable or all columns of the CSV file (neither in number nor in order).

When importing CSV data from Excel, the appropriate arguments can be determined with GetExcelArguments.

Exceptions

ArgumentNullExceptiondataTable, or filePath, or mapping is null.
ArgumentException

filePath is not a valid file path.

- or -

There is a DynamicProperty in mapping whose PropertyName finds no corresponding ColumnName in dataTable.

FormatExceptionParsing fails and Throwing is true.
NoNullAllowedExceptionThe mapping doesn't match the schema of the dataTable.
ConstraintExceptionThe parsed CSV data does not match the schema of the dataTable.
IOExceptionI/O error.
ObjectDisposedExceptionThe file was already closed.

See Also