Improve performance writing 10 million records to text file using windows service

Posted by user1039583 on Stack Overflow See other posts from Stack Overflow or by user1039583
Published on 2012-08-14T14:00:24Z Indexed on 2012/09/17 9:38 UTC
Read the original article Hit count: 212

Filed under:
|
|
|
|

I'm fetching more than 10 millions of records from database and writing to a text file. It takes hours of time to complete this operation. Is there any option to use TPL features here?

It would be great if someone could get me started implementing this with the TPL.

using (FileStream fStream = new FileStream("d:\\file.txt", FileMode.OpenOrCreate,     FileAccess.ReadWrite))
{
    BufferedStream bStream = new BufferedStream(fStream);
    TextWriter writer = new StreamWriter(bStream);
    for (int i = 0; i < 100000000; i++)
    {
        writer.WriteLine(i);
    }
    bStream.Flush();
    writer.Flush(); // empty buffer;
    fStream.Flush();
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET