Skip to content
Advertisement

Dumping SQL table to .csv C#

I am trying to implement a script in my application that will dump the entire contents (for now, but I am trying to write the code so that I can easily customize it to only grab certain columns) of a sql db (running ms sql server express 2014) to a .csv file.

Here is the code I have written currently:

Now, this was working fine when I was using it with a 3rd party SQLite-based database, but the output I’m getting after modifing this to my MSSQL db looks something like this (ITEM1 is the primary key, a standard auto-incrementing ID-field):

So it seems that it writes several entries of the same row, where I would just like one single line each row. Any suggestions?

Thanks in advance.

edit: Thanks everyone for your answers!

Advertisement

Answer

The for loop isn’t needed in the section below. Because it loops from 0 to FieldCount I assume the loop was originally meant to append the text from each column together but inside the loop there’s a single line that concatenates the text and assigns it to csvDetails.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement