Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9235473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:02:45+00:00 2026-06-18T07:02:45+00:00

Possible Duplicate: How to efficiently write to file from SQL datareader in c#? I

  • 0

Possible Duplicate:
How to efficiently write to file from SQL datareader in c#?

I am currently trying to create a web application that uses read-only access to allow users to download large files from our database. The table in question has 400,000 records in it and generates a 50 MB .csv file when exported.

It takes about 7s to run the statement “SELECT * FROM [table]” on SQL server, and about 33s to do so from my web application (hosted on a different server). This is reading all the data into a System.Data.SqlClient.SqlDataReader object.

My problem is that I am at a loss for converting my SqlDataReader to a .csv file. Converting each row of the SqlDataReader to a string and outputting that string to a file line by line takes almost 2 hours, which is unacceptable. Below is the code I’m using to create a file on the web application’s server:

    while (rdr.Read())
    {
        string lineout = "";
        for (int index = 0; index < rdr.FieldCount; index++)
            lineout += rdr[index].ToString().Replace(',', ' ') + ',';
        write(lineout, filename); //uses StreamWriter.WriteLine()
    }

There has to be a better way. I’ve looked around and saw a lot of suggestions that essentially recommend doing the above to create a file. This works great with smaller tables, but not the two really large ones we use every day. Can anyone give me a push in the right direction?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:02:46+00:00Added an answer on June 18, 2026 at 7:02 am

    You could try building your lineout with a StringBuilder rather than manually concatenating strings:

    //you can test whether it makes any difference in performance declaring a single
    //StringBuilder and clearing, or creating a new one per loop
    var sb = new StringBuilder();
    
    while (rdr.Read())
    {
        for (int index = 0; index < rdr.FieldCount; index++)
            sb.Append(rdr[index].ToString().Replace(',', ' ').Append(',');
    
        write(sb.ToString(), filename); //uses StreamWriter.WriteLine()
        sb.Clear();
    }
    

    Alternatively try to just write to the file directly and avoid generating each line in memory first:

    //assume a StreamWriter instance has been created called sw...
    while (rdr.Read())
    {
        for (int index = 0; index < rdr.FieldCount; index++)
        {
            sw.Write(rdr[index].ToString().Replace(',', ' ');
            sw.WriteLine(",");
        }
    }
    
    //flush and close stream
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C write in the middle of a binary file without overwriting any
Possible Duplicate: MATLAB: How To Efficiently Remove NaN Elements from Matrix I got code
Possible Duplicate: Most efficient way in SQL Server to get date from date+time? I
Possible Duplicate: java get file size efficiently I have a File called filename which
Possible Duplicate: Best practices for efficiently storing md5 hashes in mysql We use Hex
Possible Duplicate: How do you efficiently generate a list of K non-repeating integers between
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: Copy a file in an sane, safe and efficient way I've searched
Possible Duplicate: Can you write object oriented code in C? Object oriented programming in
Possible Duplicate: Python FAQ: How fast are exceptions? I remember reading that Python implements

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.