Export dataset to CSV

Hi,

I need to create a CSV file from SQL Server data from an ASP.NET or VB.NET application to mailmerge with MS Word.

I can populate a dataset from SQL Server and write to XML but MS Word can't use XML as a data source - damn!

Anyone got any ideas - other than cycling through the dataset and manually creating the text csv file?

Hopeful,
Steve
stevenhollidgeAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
iboutchkineConnect With a Mentor Commented:
Dim myTable As DataTable
Dim myRow As DataRow
Dim numCols As Integer = 0
Dim myString As String
Dim myWriter As New System.IO.StreamWriter("firstCSV.csv")

For Each myTable In DataSet1.Tables
For Each myRow In myTable.Rows
myString = "" For numCols = 0 To myTable.Columns.Count - 2
If Object.ReferenceEquals(myRow.Item(numCols).GetType(), myString.GetType()) Then
myString = myString & """" & myRow.Item(numCols).ToString() & ""","
ElseIf Object.ReferenceEquals(myRow.Item(numCols).GetType(), numCols.GetType()) Then
myString = myString & myRow.Item(numCols).ToString() & ","
End If

Next
If Object.ReferenceEquals(myRow.Item(numCols).GetType(), myString.GetType()) Then
myString = myString & """" & myRow.Item(numCols).ToString() & """"
ElseIf Object.ReferenceEquals(myRow.Item(numCols).GetType(), numCols.GetType()) Then
myString = myString & myRow.Item(numCols).ToString()
End If
myWriter.WriteLine(myString)
Next

Next
myWriter.Close()
0
 
Bob LearnedCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by iboutchkine

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TheLearnedOne
EE Cleanup Volunteer
0
All Courses

From novice to tech pro — start learning today.