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);

Recommended Answers

All 4 Replies

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.