DataTableExtensionReadCsvAnalyzed Method

Adds the content of a CSV file as DataRows to the DataTable after the file had been analyzed.

Definition

Namespace: FolkerKinzel.CsvTools.Mappings
Assembly: FolkerKinzel.CsvTools.Mappings (in FolkerKinzel.CsvTools.Mappings.dll) Version: 1.1.0+1263e8243dc2cd78095f678f813d7d9c52ea4315
C#
public static void ReadCsvAnalyzed(
	this DataTable dataTable,
	string filePath,
	CsvMapping mapping,
	Encoding? defaultEncoding = null,
	Header header = Header.ProbablyPresent,
	int analyzedLines = 5
)

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.
defaultEncoding  Encoding  (Optional)
The text Encoding to be used if the CSV file has no byte order mark (BOM), or null to use UTF8 in this case. Use GetExcelArguments to get the appropriate argument for this parameter when importing CSV data from Excel.
header  Header  (Optional)
A supposition that is made about the presence of a header row.
analyzedLines  Int32  (Optional)
Maximum number of lines to analyze in the CSV file. The minimum value is AnalyzedLinesMinCount. If the file has fewer lines than analyzedLines, it will be analyzed completely. (You can specify Int32.MaxValue to analyze the entire file in any case.)

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type DataTable. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

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).

The method performs a statistical analysis on the CSV file to find the appropriate parameters for reading the file. The result of the analysis is therefore always only an estimate, the accuracy of which increases with the number of lines analyzed. The analysis is time-consuming because the CSV file has to be accessed for reading.

The field delimiters COMMA (',', %x2C), SEMICOLON (';', %x3B), HASH ('#', %x23), TAB ('\t', %x09), and SPACE (' ', %x20) are recognized automatically.

This method also tries to determine the Encoding of the CSV file from the byte order mark (BOM). If no byte order mark can be found, defaultEncoding is used.

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