Posts tagged ‘OLEDB’

CSV parsing using OLEDB

For a few days I did run into the problem of importing CSV data with OLEDB. The first thing I tried was creating my own parser, but later I found an easy built in way. I show both methods here.

My own parser is simple based on the split string functionality.

//create the table
DataTable dt = new DataTable("Import");
dt.Columns.Add("col1", typeof(string));
dt.Columns.Add("col2", typeof(string));
dt.Columns.Add("col3", typeof(string));
 
//read the data
using (StreamReader sr = new StreamReader(filepath))
{
    int i = 0;
    while (!sr.EndOfStream)
    {
        //skip the header
        while (i < 1)
        {
            sr.ReadLine();
            i++;
        }
 
        string[] text = sr.ReadLine().Split(',');
        DataRow dr = dt.NewRow();
        dr["col1"] = text[0];
        dr["col2"] = text[1];
        dr["col3"] = text[2];
 
        dt.Rows.Add(dr);
    }
}

The build in CVS parser uses a connection string and a SQL query to handle the CVS data. Continue reading ‘CSV parsing using OLEDB’ »