Export data from SQL Store Procedure to text comma delimited

Hi Expert,

Please help me export data from my sql to text file with comma delimited format. I have SP that generates the data and i want it from the user interface to download the data and save is as text comma delimited.

Many Thanks.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
Can you change your stored procedure.
Dirk StraussSenior Full Stack DeveloperCommented:
The one way to do this is in SQL Server Management Studio (I'm assuming SSMS here). Export query result to .csv file in SQL Server 2008. I am assuming though that you want to do this via code. If so, just write the results of the DataSet to a CSV file: Best way to save DataSet to a CSV file.

An interesting comment on the previous article on StackOverflow was to use FileHelpers.
FileHelpers is a free and easy to use .NET library to import or export data from fixed length or delimited records in files, strings or streams.
It's free, why not give that a try?
akoITAuthor Commented:
Thanks all I already got it :)

Protected Sub ExportToCSV(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExportCSV.Click
   'Get the data from database into datatable
   Dim strQuery As String = "select CustomerID, ContactName, City," & _
           " PostalCode from customers"
   Dim cmd As New SqlCommand(strQuery)
   Dim dt As DataTable = GetData(cmd)
   Response.Buffer = True
   Response.AddHeader("content-disposition", _
           "attachment;filename=DataTable.txt") ' or csv
   Response.Charset = ""
   Response.ContentType = "application/text"
   Dim sb As New StringBuilder()
   For k As Integer = 0 To dt.Columns.Count - 1
        'add separator
         sb.Append(dt.Columns(k).ColumnName + ","c)
   'append new line
   sb.Append(vbCr & vbLf)
   For i As Integer = 0 To dt.Rows.Count - 1
      For k As Integer = 0 To dt.Columns.Count - 1
        'add separator
         sb.Append(dt.Rows(i)(k).ToString().Replace(",", ";") + ","c)
     'append new line
      sb.Append(vbCr & vbLf)
End Sub

credits to https://www.aspsnippets.com :)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dirk StraussSenior Full Stack DeveloperCommented:
I feel that there should be an accepted/assisted solution. The author did not respond to any of the experts.
akoITAuthor Commented:
Hi Dirk,

Sorry for I do not have response to any of the expert solution because  there is no assisted solution on the current problem.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.