Parsing CSV using OleDb using C#

Zulfi Tapia picture Zulfi Tapia · Jul 25, 2011 · Viewed 45.6k times · Source

I know this topic is done to death but I am at wits end.

I need to parse a csv. It's a pretty average CSV and the parsing logic has been written using OleDB by another developer who swore that it work before he went on vacation :)

CSV sample:
Dispatch Date,Master Tape,Master Time Code,Material ID,Channel,Title,Version,Duration,Language,Producer,Edit Date,Packaging,1 st TX,Last TX,Usage,S&P Rating,Comments,Replace,Event TX Date,Alternate Title
,a,b,c,d,e,f,g,h,,i,,j,k,,l,m,,n,

The problem I have is that I get various errors depending on the connection string I try.

when I try the connection string:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source="D:\TEST.csv\";Extended Properties="text;HDR=No;FMT=Delimited"

I get the error:

'D:\TEST.csv' is not a valid path.  Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.

When I try the connection string:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\TEST.csv;Extended Properties=Excel 12.0;

or the connection string

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\TEST.csv;Extended Properties=Excel 8.0;

I get the error:

External table is not in the expected format.

I am considering throwing away all the code and starting from scratch. Is there something obvious I am doing wrong?

Answer

Darin Dimitrov picture Darin Dimitrov · Jul 25, 2011

You should indicate only the directory name in your connection string. The file name will be used to query:

var filename = @"c:\work\test.csv";
var connString = string.Format(
    @"Provider=Microsoft.Jet.OleDb.4.0; Data Source={0};Extended Properties=""Text;HDR=YES;FMT=Delimited""", 
    Path.GetDirectoryName(filename)
);
using (var conn = new OleDbConnection(connString))
{
    conn.Open();
    var query = "SELECT * FROM [" + Path.GetFileName(filename) + "]";
    using (var adapter = new OleDbDataAdapter(query, conn))
    {
        var ds = new DataSet("CSV File");
        adapter.Fill(ds);
    }
}

And instead of OleDB you could use a decent CSV parser (or another one).