Reading large .csv file loading is very slow.

First let me state that I am very new to C#. I learned C++ in college but that was in 99 and have been coding mainly mainframe since then. So I am kind of new to the .Net platform as well. The problem that I am having is when I am loading in a .csv file it is taking a long time before the user can do anything because the program is busy loading my data into my array. Is there a better way for me to do a read then using StreamReader. Here is a snipet of code where I am doing the load. Any help would be greatly appreciated.

 

private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)

{

string line;

bool newCustomer;

//int i = 0;

txtPath.Text = openFileDialog1.FileName.ToString();

StreamReader sr = new StreamReader(openFileDialog1.FileName);

qb.addCust();

while ((line = sr.ReadLine()) != null)

{

newCustomer = qb.loadCust(line);

if (newCustomer == true)

qb.addCust();

else

txtInside.Text = txtInside.Text + "\n" + qb.cust[0];

}

sr.Close();

}




Answer this question

Reading large .csv file loading is very slow.

  • Anuradha Rawal

    I would recommend using a predone library such as this one http://www.codeproject.com/cs/database/CsvReader.asp which has already spent the time to optimize such conditions.


  • Jorge Stalin

    How do you do a read with one go I was wondering if there was a way to determine the size of the file before reading it. Then perhaps breaking them into chunks into a buffer area Then reading the buffers and clearing it out. If anyone knows if this is possible and if they have some code examples I would appreciate it.

  • Shawn K.

    You should probably create a separate thread to do the I/O and return to the user after starting the thread. Of course, you'd need some kind of synchronization...

    -Matteo



  • trenthaynes

    You can try to parse it with the FileHelpers library too

    http://filehelpers.sourceforge.net

    you need to define a class for the records:

    [DelimitedRecord("\"")]
    public YourClass
    {
    [FiledQuoted()]
    public string First;
    [FiledQuoted()]
    public string Second..

    ....
    }

    and then use the library like this.

    FIleHelpersAsyncEngine engine = new FileHelperAsyncEngine(typeof(YourClass));

    engine.BeginReadFile("file.txt");

    while(engine.ReadNext() != null)
    {
    you get your record in engine.LastRecord

    // You can show progress messages for the users

    }
    engine.EndsRead();

    Best Regards


  • zulluz

    Reading the entire file in one go may be a bit faster, then parsing it all in memory. Otherwise, you could buy a 7200 RPM hard drive :-)



  • Sixfore

    Thanks for responding. There isnt a way for me to read the file more quickly

  • LeeDan

    You can read it in one go with File.ReadAllLines(). You can find out the file size first, but what you're suggesting is very messy. Reading all in one go may be faster, but I doubt it would be by much. Playing with buffers will not be faster, I don't believe.



  • Reading large .csv file loading is very slow.