954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem parsing c#

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?

udigold1
Newbie Poster
19 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

what is your csv data?

sandeepparekh9
Posting Whiz
367 posts since Dec 2010
Reputation Points: 123
Solved Threads: 71
 

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

Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 
Csv files uses as a delimiter comma ",", or semicolon ";". Try with one of them.

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

udigold1
Newbie Poster
19 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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

item1=item2=item3


Or not?

Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 

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,

udigold1
Newbie Poster
19 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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

Bridgekeeper
Newbie Poster
8 posts since Sep 2010
Reputation Points: 9
Solved Threads: 3
 

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.

alc6379
Cookie... That's it
Team Colleague
2,820 posts since Dec 2003
Reputation Points: 186
Solved Threads: 147
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: