Private Sub ExportDatasetToCsv(ByVal MyDataSet As DataSet)
Dim dt As DataTable
Dim dr As DataRow
Dim MyString As String
Dim bFirstRecord As Boolean = True
Dim myWriter As New System.IO.StreamWriter("C:\MyTestCSV.csv")
MyString = ""
Try
For Each dt In MyDataSet.Tables()
For Each dr In dt.Rows
bFirstRecord = True
For Each field As Object In dr.ItemArray
If Not bFirstRecord Then
MyString.AppendText(",")
End If
MyString.AppendText(field.ToString)
bFirstRecord = False
Next
'New Line to differentiate next row
MyString.AppendText(Environment.NewLine)
Next
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
'Write the String to the Csv File
myWriter.WriteLine(MyString)
'Clean up
myWriter.Close()
End Sub |