2

I'm looking for the fastest way to read data from a CSV file. I tried doing this two different ways.

Method #1: Reading all of the lines from the CSV file into an array an then iterating the array:

String[] csv = File.ReadAllLines(@"E:\be.csv");

for (int i = 0; i < csv.Length; i++)
{
    tx.Text = csv[i];
    tx.Refresh();
}

Method #2*: Using StreamReader:

StreamReader sr = new StreamReader(new FileStream(@"E:\be.csv");

while (!sr.EndOfStream)
{
    string seg = sr.ReadLine();
    tx.Text = sr.ReadLine();
    tx.Refresh();
}

Using StreamReader seems to be a lot faster. Is there an even faster method to import data from a CSV file?

DavidRR
  • 18,291
  • 25
  • 109
  • 191
BenG
  • 53
  • 1
  • 1
  • 9
  • 5
    Don't try to parse it yoruself, just use a CSV parser, that way you don't need to be parsing the string yourself, and dealing with all of the weird issues (quote escaping, for example) and can just focus on using the data. And yes, whenever dealing with any files, ever, you should be streaming the data unless there is a compelling reason not to. – Servy Dec 18 '13 at 19:41
  • 1
    Fast is usually not what you need to worry about with CSV. Correct is. You may assume that all you need to do is split on commas, but this will get you into a world of hurt. Google some [CSV Parsers](http://stackoverflow.com/questions/906841/csv-parser-reader-for-c) – crthompson Dec 18 '13 at 19:44
  • 1
    Also, what is `tx`, and why does it make sense to refresh it within a loop? – John Saunders Dec 18 '13 at 19:45
  • In my real program I've got a dynamically changing input, so I added a Timer.Tick Event which starts to read in the file again in every few seconds and tx is a TextBox which displays the data that I've read in. – BenG Dec 18 '13 at 19:59

3 Answers3

3

I must suggest CsvHelper which can be found in NuGet. Having used it for the first time yesterday, it will now be my go to solution.

http://www.nuget.org/packages/CsvHelper/

For your actual question of what's faster, I would probably say just doing File.ReadAllLines(...), but it would be best for you to actually try and set up a test to do both options several hundred times and see which one took the longest.

Also, do not forget to dispose your StreamReader. Go ahead and wrap that in a using statement so everything gets closed/disposed probably.

TyCobb
  • 8,909
  • 1
  • 33
  • 53
3

Sebastien Loren's Fast CSV Reader on CodeProject is probably the way to go.

It implements IDataReader and acts mostly like a StreamReader.

Highly recommended.

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
1

.NET has a built in CSV parser called TextFieldParser.

DavidRR
  • 18,291
  • 25
  • 109
  • 191
Sam Axe
  • 33,313
  • 9
  • 55
  • 89