Efficiently loads CSV data to .NET DATATABLE
Loading CSV data to DataTable is a common task in data processing and analysis. This article discusses how to use the built -in function of .NET to achieve this goal, focusing on solving the problem of creating architecture from the CSV file.
Standard ADO.NET framework does not directly support reading CSV files. However, we can use the
<.> Use the .ini file to create architecture
To create a architecture file (SCHEMA.ini), follow the following steps:
Put the following code in a file named SCHEMA.INI:
<code>[FileName.csv] Format=CSVDelimited ColNameHeader=True MaxScanRows=0</code>
This method uses the CSV file path and a logo as an input. This sign indicates whether the first line contains the title. It returns a filled DataTable based on CSV data.
This method accepts the CSV file path and a Boolean value as a parameter, indicating whether the first line is the header. It returns a DataTable that fills CSV data. The improved code increased the reference of
andusing System.Data; using System.Data.OleDb; using System.Globalization; using System.IO; static DataTable GetDataTableFromCsv(string path, bool isFirstRowHeader) { string header = isFirstRowHeader ? "Yes" : "No"; string pathOnly = Path.GetDirectoryName(path); string fileName = Path.GetFileName(path); string sql = @"SELECT * FROM [" + fileName + "]"; using (OleDbConnection connection = new OleDbConnection( @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly + ";Extended Properties=\"Text;HDR=" + header + "\"")) using (OleDbCommand command = new OleDbCommand(sql, connection)) using (OleDbDataAdapter adapter = new OleDbDataAdapter(command)) { DataTable dataTable = new DataTable(); dataTable.Locale = CultureInfo.CurrentCulture; adapter.Fill(dataTable); return dataTable; } }
The above is the detailed content of How to Efficiently Load CSV Data into a .NET DataTable?. For more information, please follow other related articles on the PHP Chinese website!