How to create a csv from a datatable In a project I’ve been working on I came across the need export a DataTable to CSV and this is how I did it. I have split up the functionality as I also use them to concatenate several DataTables into one CSV. I need to import for following namespaces in this example. using System.Data.SqlClient; using System.Text; The first method gets the DataTable. It could easily be changed to use stored procedures and add parameters. private DataTable GetDataTable() { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString); SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM MyTable", con); DataTable dt = new DataTable(); using (con) { da.Fill(dt); } return dt; } The next method creates the CSV using a StringBuilder. First it create...