Damn, this is so much easier in a form...Anyone know how to export from a DataSet to Csv file in a console application?

            string sp = "SLA2Day";
            var outCsvFile = @"C:\Download Report Sheets\file.txt";

            SqlCommand spcmd = new SqlCommand(sp, thisConnection);
            spcmd.CommandType = CommandType.StoredProcedure;

            DataSet dsData = new DataSet();
            DataTable dt = new DataTable();

            SqlDataAdapter da = new SqlDataAdapter(spcmd);

            thisConnection.Open();
            da.Fill(dsData);

How is it any different? What's your code for exporting in a Form? And what's not working about it in a console application? You might simply be missing references...

Im using this:

File.WriteAllText(outCsvFile, dt.Columns[0].ToString());

but I actually want to export everything in the dataset to the CSV file.

Use a streamwriter and foreach through the data set? That's how I'd do it

Edited 4 Years Ago by Mike Askew

Code here:

    System.Data.OleDb.OleDbConnection oleDbConnection1
         = new System.Data.OleDb.OleDbConnection();
    oleDbConnection1.ConnectionString
        = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=File Path";

    System.Data.OleDb.OleDbCommand oleDbCommand1
        = new System.Data.OleDb.OleDbCommand();
    oleDbCommand1.CommandText = "select * from Table";
    oleDbCommand1.Connection = oleDbConnection1;

    Spire.DataExport.TXT.TXTExport txtExport1
        = new Spire.DataExport.TXT.TXTExport();

    txtExport1.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView;
    txtExport1.DataEncoding = Spire.DataExport.Common.EncodingType.ASCII;
    txtExport1.SQLCommand = oleDbCommand1;

    oleDbConnection1.Open();

    txtExport1.ExportType = Spire.DataExport.TXT.TextExportType.CSV;
    txtExport1.FileName = "sample.csv";
    txtExport1.SaveToFile();

DataExport component used.

This article has been dead for over six months. Start a new discussion instead.