title: c# Build very large text file from SQL query
body: <p>I am taking 100+ tables from a SQL Database and writing all the data into 1 file.  In SQL, I ran a complex query to format the tables into one view. 
  In VS, I am connecting to SQL and selecting * from the view and using String Builder to build the file.</p>
  
  <p>This works if I only grab 5% of the data and crashes with a memory exception if i try to grab 100%.  The view has 10 million+ records.  I am looking for advice.  Thanks.</p>
  
  <pre><code>string cs4 = ConfigurationManager.ConnectionStrings["example"].ConnectionString;
  StringBuilder sb4 = new StringBuilder();
  using (SqlConnection con4 = new SqlConnection(cs4))
  {
      string strTQuery = @"SELECT * FROM [dbo].[view]";
      SqlDataAdapter da4 = new SqlDataAdapter(strTQuery, con4);
      DataSet ds4 = new DataSet();
      da4.Fill(ds4);
  
      ds4.Tables[0].TableName = "Example";
  
      foreach (DataRow exDR in ds4.Tables["Example"].Rows)
      {
          sb4.Append(exDR["A"].ToString() + strDelimiter);
          sb4.Append(exDR["B"].ToString() + strDelimiter);
          sb4.Append(exDR["C"].ToString() + strDelimiter);
          sb4.Append(exDR["D"].ToString());
          sb4.Append("\r\n");
      }
  }
  
  StreamWriter file4 = new StreamWriter(@"\\Desktop" + todaysDate + ".csv");
  file4.WriteLine(sb4.ToString());
  file4.Close();
  </code></pre>