Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Best way to read a CSV file

Status
Not open for further replies.

ppat60

Programmer
Oct 20, 2007
13
0
0
US
Hi I am very new to C# that my first learning curve. :) My question is How do I read a CSV(Comma Separated Value) file to be processed? Or should I ask what is the best way to process a CSV?

Thank you in advance.
 
Create a streamreader (TextReader) and read the file line by line. You can then split each line up via commas. As long as your CSV file is formatted correctly you should be able to parse the information out of the file easily after that.


string line = textreader1.ReadLine();
string[] items = line.Split(',');

int ID = Convert.ToInt32(items[0]);
string name = items[1];
...

The other thing you could do is create a DataTable from the Data and as you read each line, dump it into that table. Some people have a DataTable fetish or sorts...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top