Hi,

I'm trying to parse a csv fiel using a code i've found on line:

private  DataTable ParseCSV(string path)
        {
            if (!File.Exists(path))
                return null;

            string full = Path.GetFullPath(path);
            string file = Path.GetFileName(full);
            string dir = Path.GetDirectoryName(full);

            //create the "database" connection string 
            string connString = "Provider=Microsoft.Jet.OLEDB.4.0;"
              + "Data Source=\"" + dir + "\\\";"
              + "Extended Properties=\"text;HDR=No;FMT=Delimited(=)\"";


            //create the database query
            string query = "SELECT * FROM " + file;

            //create a DataTable to hold the query results
            DataTable dTable = new DataTable();

            //create an OleDbDataAdapter to execute the query
            OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);

            try
            {
                //fill the DataTable
                dAdapter.Fill(dTable);
            }
            catch (InvalidOperationException /*e*/)
            { }

            dAdapter.Dispose();

            dataGridView1.DataSource = dTable;
            return dTable;
        }

Only problem is that my delimiter is an equal sign (=), and the code doesn't delimit it even though i tried to put the equal sign. I just get one column with the undelimited data, instead of two seperate columns.

Why is this happening? is there a solution for that? or is there other way of parsing csv right?

Csv files uses as a delimiter comma ",", or semicolon ";".
Try with one of them.

my Delimiter is a '=' sign, not comma or semicolon...

And in that one column, there are Equal marks as well?
you have that column as example:

item1=item2=item3

Or not?

And in that one column, there are Equal marks as well?
you have that column as example:

item1=item2=item3

Or not?

Hi, i figured out how to handle this, using schema.ini file.
But now I got another issue: the header of the dataset/datatable is actually the first line of the csv file, and so when i write the dataset to the access file, I actually miss writing the first line because it's a header. do you know how to make into a regular line and not a header?

Thanks,

This article has been dead for over six months. Start a new discussion instead.