Feedback

CSV Datei in Datatable einlesen

Sprache: C#

CSV Datei in Datatable einlesen
// using System.Data.Odbc;
// using System.IO;

private DataTable GetTableFromCSV(string path)
{
    if (!File.Exists(path))
        throw new FileNotFoundException();
   
    FileInfo fileInfo = new FileInfo(path);
    DataTable dataTable = new DataTable();
    string connectionString = String.Format("Driver={{Microsoft Text Driver (*.txt; *.csv)}};Dbq={0};", fileInfo.DirectoryName);
    OdbcConnection connection = new OdbcConnection(connectionString);
    OdbcDataAdapter da = new OdbcDataAdapter(String.Format("select * from [{0}]",fileInfo.Name), connection);
    da.Fill(dataTable);
    return dataTable;
}
// using System.Data.Odbc;
// using System.IO;

private DataTable GetTableFromCSV(string path)
{
    if (!File.Exists(path))
        throw new FileNotFoundException();
   
    FileInfo fileInfo = new FileInfo(path);
    DataTable dataTable = new DataTable();
    string connectionString = String.Format("Driver={{Microsoft Text Driver (*.txt; *.csv)}};Dbq={0};", fileInfo.DirectoryName);
    OdbcConnection connection = new OdbcConnection(connectionString);
    OdbcDataAdapter da = new OdbcDataAdapter(String.Format("select * from [{0}]",fileInfo.Name), connection);
    da.Fill(dataTable);
    return dataTable;
}