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?

Recommended Answers

All 7 Replies

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

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,

Please post an example of your data in order to help.

Sounds like you need to manually parse this CSV file. OLE Providers are meant for pretty "standard" data sets, where there is a real header, and those header names are used for the names of the columns in the data set.

Try taking a look at this:

http://stackoverflow.com/questions/448865/how-can-i-best-parse-this-comma-delimited-text-file

That might give you some ideas on how to manually parse the file, and use a collection of string arrays, or something.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.