Home > Backend Development > C++ > How to Efficiently Load CSV Data into a .NET DataTable?

How to Efficiently Load CSV Data into a .NET DataTable?

Barbara Streisand
Release: 2025-01-27 19:31:08
Original
985 people have browsed it

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.

How to Efficiently Load CSV Data into a .NET DataTable?

Use standard Ado.NET function

Standard ADO.NET framework does not directly support reading CSV files. However, we can use the OLEDB

provider to introduce CSV data by creating architecture definition files.

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

  1. Save the file in the same directory as the CSV file.
<code>[FileName.csv]
Format=CSVDelimited
ColNameHeader=True
MaxScanRows=0</code>
Copy after login
    code implementation
The following code demonstrates how to use the OLEDB provider with a architectural definition file to load CSV data to DataTable:

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

and
using 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;
    }
}
Copy after login
naming space, and more clearly displayed the function of the code.

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template