title: How can I turn a DataTable to a CSV?
body: <p>Could somebody please tell me why the following code is not working.  The data is saved into the csv file, however the data is not separated.  It all exists within the first cell of each row. </p>
  
  <pre><code>StringBuilder sb = new StringBuilder();
  
  foreach (DataColumn col in dt.Columns)
  {
      sb.Append(col.ColumnName + ',');
  }
  
  sb.Remove(sb.Length - 1, 1);
  sb.Append(Environment.NewLine);
  
  foreach (DataRow row in dt.Rows)
  {
      for (int i = 0; i &lt; dt.Columns.Count; i++)
      {
          sb.Append(row[i].ToString() + ",");
      }
  
      sb.Append(Environment.NewLine);
  }
  
  File.WriteAllText("test.csv", sb.ToString());
  </code></pre>
  
  <p>Thanks.</p>