title: Fast way of writing SQL server table into file
body: <p>I have a remote SQL server. I want to make a <em>local</em> copy of all tables in this server. I don't care about the file format used locally, I'm looking for the fastest approach of getting the data from SQL server into the file. <em>(note: server side backup is not an option)</em></p>
  
  <p>This is my current approach:</p>
  
  <p><strong>Step 1</strong>. Creating a reader and read all data into a List of objects</p>
  
  <pre><code>while (reader.Read()) {
  
      var fieldCount = reader.FieldCount;
      for (int i = 0; i &lt; fieldCount; i++) {
          objects.Add(reader.GetValue(i));
      }
  }
  </code></pre>
  
  <p><strong>Step 2</strong>. Convert the objects to strings</p>
  
  <pre><code>List&lt;string&gt; test = new List&lt;string&gt;();
  foreach (var o in objects) {
      test.Add(o.ToString());
  }
  </code></pre>
  
  <p><strong>Step 3</strong>. Write the string to a (CSV) file</p>
  
  <pre><code>foreach (var s in test) {
      backupFile.Write("\"");
      backupFile.Write(s);
      backupFile.Write("\";");
  }
  </code></pre>
  
  <p>I've measured the performance of these 3 steps:</p>
  
  <ul>
  <li>Step 1 takes 3 seconds </li>
  <li>Step 2 takes <strong>2.8 seconds</strong> </li>
  <li>Step 3 takes 0.8 seconds</li>
  </ul>
  
  <p>I'm looking for a way to speed up step 2. Is there a faster way of getting these objects to a file? (doesn't have to be a text file. Binary of local database file is also ok)</p>