3

i try to inport csv file to my database in my C# winform program.

for example the csv file:

code  ,name    ,price
101010,computer,200$

and i done this:

char[] BI = { ',' };
string[] WI = TEMP.Split(BI);
A = WI[0].Trim().ToString();
B = WI[1].Trim().ToString();
C = WI[2].Trim().ToString();

but what i can do if the name contain (,) ?

for example

code  ,name              ,price
101010,computer 12,200.00,200$

if i get this type of csv:

code    ,name                ,price
"101010","computer 12,200.00","200$"

how to deal with this ?

2 Answers 2

8

The best thing to do is not roll your own csv and use a existing free library. I recommend FileHelpers.

EDIT:

Here is a fun little read, I bet you are on step 2 or step 3.

Sign up to request clarification or add additional context in comments.

Comments

5

Have a look at http://www.codeproject.com/KB/database/CsvReader.aspx, I believe this solves your problem.

3 Comments

I use CsvReader when Filehelpers has issues.
@Scott, what issue would those be, and wouldn't it be best to report them to the author?
@he_the_great I had some issues with non printable characters in the csv (caused from a export from a corrupted database) i was importing. CSVReader was simpler to dive in to and edit so i could break on the bad lines and skip over them

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.