Hello,
I want to Export SQL data into .CSV file from Query.can you give me advise how to do it.
Thanks in advanse.
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Feb 18, 2012, 3:45 AM EST
Shubham SrivastavaRank #290 · 8.7K reputationPosted Feb 18, 2012, 11:43 PM EST
Suthish NairRank #92 · 22.8K reputationPosted Feb 19, 2012, 10:31 AM EST
Jignesh TrivediRank #16 · 50.3K reputationPosted Feb 20, 2012, 5:42 PM EST
please refer,
http://www.simple-talk.com/sql/database-administration/creating-csv-files-using-bcp-and-stored-procedures/
http://www.codeproject.com/Articles/9495/A-Simplified-SQL-CSV-Import-Export-Functionality
http://www.virtualobjectives.com.au/sqlserver/saving_to_csv.htm
hope this help.
David SmithRank #999 · 2K reputationPosted Mar 8, 2012, 4:53 AM EST
David SmithRank #999 · 2K reputationPosted Mar 8, 2012, 5:07 AM EST
Lets say I have a Database call Named or called ColorDatabase and the table name ColorTable how to put this in the syntax below, I am struggling
BCP master..sysobjects out c:\sysobjects.txt -c -t, -T –S<servername>
VulpesRank #55 · 29.2K reputationPosted Mar 8, 2012, 11:46 AM EST
http://msdn.microsoft.com/en-us/library/ms162802.aspx
As you'll see the first parameter is the name of the table which in your example will be ColorDatabase.ColorTable.
After 'out' you need the path of the text file you're going to create.
The other parameters should be self-explanatory.
Gohil JayendrasinhRank #490 · 5K reputationPosted Mar 8, 2012, 7:13 PM EST
if you use SSMS 2008
then
Tools->Options->Query Results->Sql Server->Results To Grid
Quote strings containing list separators when saving .csv results
Harshil Shah9 reputationPosted Mar 8, 2012, 10:03 PM EST
When i am using BCP,below error occur.
Error = [Microsoft][SQL Native Client]An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections.
Thanks in advanse.
David SmithRank #999 · 2K reputationPosted Mar 9, 2012, 5:10 AM EST
BEGIN
DECLARE @sql varchar(8000)
SET @sql = 'bcp "SELECT * FROM ColorDatabase.dbo.ColorTable" queryout "C:\Temp\Example.txt" -c -t; -T -SXXXXXXXXXX'
Print @sql
exec master..xp_cmdshell @sql
END
George ericRank #9,604 · 204 reputationPosted Apr 9, 2012, 7:26 PM EDT
Below is the code
using (OleDbConnection oleDbConnection = new OleDbConnection())
{
oleDbConnection.ConnectionString = this.textBox1.Text;
OleDbCommand oleDbCommand = new OleDbCommand();
oleDbCommand.CommandText = this.textBox2.Text;
oleDbCommand.Connection = oleDbConnection;
using (OleDbDataAdapter da = new OleDbDataAdapter(oleDbCommand))
{
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
}
SenthilkumarRank #387 · 6.5K reputationPosted Apr 9, 2012, 8:33 PM EDT
I think the user wants to export the data into csv from the query window itself.
He is not expecting to do in the code level. If i am right then its one step to do this.
write the query and get the result in the grid format.
right click on the result area
Save it as .csv
Rajesh Kumar128 reputationPosted Jan 31, 2013, 9:02 PM EST
Harshil Shah9 reputationPosted Jan 31, 2013, 9:21 PM EST
please refer,
http://www.simple-talk.com/
http://www.codeproject.com/
http://www.virtualobjectives.
for header try View instead of query using SQL BCP.
write View like this
hope this help.