Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# application to sort SQL database data and output to CSV file

C# application to sort SQL database data and output to CSV file

Scheduled Pinned Locked Moved C#
databasecsharpquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    totally_stumped
    wrote on last edited by
    #1

    Hi there I am having problems developing a C# application that sorts the data in an SQL database by a particular column and then outputs it to a csv file on my hard drive. I want to do something like this:

    SqlCommand sqlComm = new SqlCommand("SELECT * FROM TableA ORDER BY ColumnB
    INTO OUTFILE ‘C:\output_file.csv’", myConnection);

    Is this the right way to go about it? Thanks in advance

    D S P 3 Replies Last reply
    0
    • T totally_stumped

      Hi there I am having problems developing a C# application that sorts the data in an SQL database by a particular column and then outputs it to a csv file on my hard drive. I want to do something like this:

      SqlCommand sqlComm = new SqlCommand("SELECT * FROM TableA ORDER BY ColumnB
      INTO OUTFILE ‘C:\output_file.csv’", myConnection);

      Is this the right way to go about it? Thanks in advance

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      totally_stumped wrote:

      SELECT * FROM TableA ORDER BY ColumnB INTO OUTFILE ‘C:\output_file.csv’

      Doesn't looks like it will execute on SQL server. AFAIK, you will need to use BCP to export data to csv file. Or, you can get the data from database in your code and then write that to CSV.

      50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

      1 Reply Last reply
      0
      • T totally_stumped

        Hi there I am having problems developing a C# application that sorts the data in an SQL database by a particular column and then outputs it to a csv file on my hard drive. I want to do something like this:

        SqlCommand sqlComm = new SqlCommand("SELECT * FROM TableA ORDER BY ColumnB
        INTO OUTFILE ‘C:\output_file.csv’", myConnection);

        Is this the right way to go about it? Thanks in advance

        S Offline
        S Offline
        Shorgov
        wrote on last edited by
        #3

        You should know that the sql command is executed on the computer where the MS SQL server is installed. This means that "C:\output_file.csv" is server's C drive. You may have no access to it! The second problem is the query itself. I think that this query is only available for MySQL user. A better approach will be to get the data from the server into a DataSet (on client side) and write a simple method to convert the data to scv file. I am quite sure that an article explaining how to do this exists in CP's articles.

        1 Reply Last reply
        0
        • T totally_stumped

          Hi there I am having problems developing a C# application that sorts the data in an SQL database by a particular column and then outputs it to a csv file on my hard drive. I want to do something like this:

          SqlCommand sqlComm = new SqlCommand("SELECT * FROM TableA ORDER BY ColumnB
          INTO OUTFILE ‘C:\output_file.csv’", myConnection);

          Is this the right way to go about it? Thanks in advance

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Maybe you should ask in the database forum. :-D But seriously; you'll need to write some code. Use a DataReader to read the data, then write each row to the file. It's not a big deal.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups