C# application to sort SQL database data and output to CSV file
-
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
-
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
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...!!
-
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
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.
-
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
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.