Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient function for reading a delimited file into DataTable

Tags:

c#

.net

file

import

I was wondering if anyone knew of an efficient c# function for reading a tab delimited file into a datatable?

Thanks

like image 803
AlteredConcept Avatar asked Jul 22 '09 15:07

AlteredConcept


2 Answers

This currently uses the LINQ methods .First() and .Skip() both are easy to recreate if you need to use this on .Net 2.0

//even cooler as an extension method
static IEnumerable<string> ReadAsLines(string filename)
{
    using (var reader = new StreamReader(filename))
        while (!reader.EndOfStream)
            yield return reader.ReadLine();
}

static void Main()
{
    var filename = "tabfile.txt";
    var reader = ReadAsLines(filename);

    var data = new DataTable();

    //this assume the first record is filled with the column names
    var headers = reader.First().Split('\t');
    foreach (var header in headers)
        data.Columns.Add(header);

    var records = reader.Skip(1);
    foreach (var record in records)
        data.Rows.Add(record.Split('\t'));
}
like image 162
Matthew Whited Avatar answered Sep 21 '22 18:09

Matthew Whited


public System.Data.DataTable GetDataTable(string strFileName)
{
    System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + System.IO.Path.GetDirectoryName(strFileName) + ";Extended Properties = \"Text;HDR=YES;FMT=TabDelimited\"");
    conn.Open();
    string strQuery = "SELECT * FROM [" + System.IO.Path.GetFileName(strFileName) + "]";
    System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(strQuery, conn);
    System.Data.DataSet ds = new System.Data.DataSet("CSV File");
    adapter.Fill(ds);
    conn.Close();
    return ds.Tables[0];
 }
like image 34
Arsen Mkrtchyan Avatar answered Sep 20 '22 18:09

Arsen Mkrtchyan