Export sql data to xml and csv I have the below code that takes the sql data and exports it to xml or csv. The xml is processing at good speed but my csv sometimes takes 1 hour depending on how many data rows it exports. My question is my code effective writing the csv or it can be improved for speed? Has around 40 columns and x rows which can be over 10k. 

public static void SqlExtract(this string queryStatement, string xFilePath, string fileName)
        {
            string connectionString = @"Data Source=ipaddress; Initial Catalog=name; User ID=username; Password=password";
            using (SqlConnection _con = new SqlConnection(connectionString))
            {
                using (SqlCommand _cmd = new SqlCommand(queryStatement, _con))
                {
                    using (SqlDataAdapter _dap = new SqlDataAdapter(_cmd))
                    {
                        if (fileName == "item1" || fileName == "item2" || fileName == "item3")
                        {
                            DataSet ds = new DataSet("FItem");
                            _con.Open();
                            _dap.Fill(ds);
                            _con.Close();
                            FileStream fs = new FileStream(xFilePath, FileMode.Create, FileAccess.Write, FileShare.None);
                            StreamWriter writer = new StreamWriter(fs, Encoding.UTF8);
                            ds.WriteXml(writer, XmlWriteMode.IgnoreSchema);
                            fs.Close();
                            StringWriter sw = new StringWriter();
                            ds.WriteXml(sw, XmlWriteMode.IgnoreSchema);
                            string OutputXML = sw.ToString();
                            OutputXML = OutputXML.Replace("Table", "Item");
                            System.IO.File.WriteAllText(xFilePath, OutputXML);
                        }
                        else
                        {
                            DataTable table1 = new DataTable("Table1");
                            _con.Open();
                            _dap.Fill(table1);
                            _con.Close();
                            string exportCSV = string.Empty;
                            foreach (DataRow row in table1.Rows)
                            {
                                int i = 1;
                                foreach (DataColumn column in table1.Columns)
                                {
                                    if (row[1].ToString() == "002" &amp;&amp; i &gt; 41 || row[1].ToString() == "END" &amp;&amp; i &gt; 4)
                                    {
                                        //do nothing
                                    }
                                    else
                                    {
                                        if (i &gt; 1)
                                        {
                                            exportCSV += ";";
                                        }
                                        exportCSV += row[column.ColumnName].ToString();
                                    }
                                    i++;
                                }
                                exportCSV += "\r\n";
                            }
                            //Write CSV
                            File.WriteAllText(xFilePath, exportCSV.ToString());
                        }
                    }
                }
            }
        }
Some quick remarks:


Don't start variable names with underscores unless they're class-wide private ones.
Local variables should be camelCased. OutputXML doesn't follow that rule.
Split your code into smaller methods that don't intermix data retrieval and file writing. Your method does multiple things, depending on some (odd) logic. Even its name doesn't make sense: it's called SqlExtract yet it generates CSV or XML files depending on seemingly arbitrary conditions.
Don't hardcode your connection string. Put it in a .config file and access it via the ConfigurationManager.
Why do _con.Open(); and _con.Close();? The using takes care of this.
Don't loop through table1.Rows. Instead have a method that transforms a DataRow row into a csv line and use that to construct a IEnumerable&lt;T&gt;, which you then can use in combination with string.Join(). Something like var csvContents = string.Join(Environment.NewLine, table1.Rows.Cast&lt;DataRow&gt;().Select(x =&gt; DataRowToCsvLine(x)));. That's one line (instead of 20) which expresses far better what your code does (and yes, the DataRowToCsvLine method is also a couple of lines, but it doesn't pollute the main logic).
row[1].ToString() == "END": are you sure it is always going to be "END"? Or is it possible it can be "end"? Consider using string.Equals() instead, which allows for case insensitive comparisons. Same for fileName == "item1" etc.
Properly dispose of StringWriter with a using block. Example: https://social.msdn.microsoft.com/Forums/vstudio/en-US/7d949d5c-a41c-4dc7-bbcb-429761f851d1/is-calling-flush-and-close-necesary-for-stringwriter-and-xmltextwriter?forum=netfxbcl


None of the above might improve the speed of your code, but it will make it much more maintainable and consistent.