Export a Dataset to a csv file.
-
I dont think this wouldbe to difficult but can anyone provide me a piece of code to do this: I would like the end result to look like this:
ID,MyName,MyAddrerss,MyAge ect...
Here is one possible method to write a DataTable to a csv file. If you want to write out a whole DataSet to csv files, then you could loop through each table in your DataSet and produce a file for each table. Hope this helps :)
Public Sub WriteCsv(dt as datatable, filename as string, delimiter as char)
dim writer as System.IO.StreamWriter try writer = new System.IO.StreamWriter(filename) 'write the column names for i as integer = 0 to dt.Columns.Count - 1 writer.write(dt.Columns(i).ColumnName) if i <> dt.Columns.Count - 1 then writer.write (delimiter) next writer.write(controlchars.newline) 'write the data For i As Integer = 0 To dt.Rows.Count - 1 For j As Integer = 0 To dt.Columns.Count - 1 writer.Write(dt.Rows(i).Item(j)) If j <> dt.Columns.Count - 1 Then writer.Write(delimiter) Next if i <> dt.Rows.Count - 1 then writer.Write(ControlChars.NewLine) Next writer.Flush() writer.Close() writer = Nothing Catch ex As Exception If Not writer Is Nothing Then writer = Nothing End If End Try
End Sub
-
Here is one possible method to write a DataTable to a csv file. If you want to write out a whole DataSet to csv files, then you could loop through each table in your DataSet and produce a file for each table. Hope this helps :)
Public Sub WriteCsv(dt as datatable, filename as string, delimiter as char)
dim writer as System.IO.StreamWriter try writer = new System.IO.StreamWriter(filename) 'write the column names for i as integer = 0 to dt.Columns.Count - 1 writer.write(dt.Columns(i).ColumnName) if i <> dt.Columns.Count - 1 then writer.write (delimiter) next writer.write(controlchars.newline) 'write the data For i As Integer = 0 To dt.Rows.Count - 1 For j As Integer = 0 To dt.Columns.Count - 1 writer.Write(dt.Rows(i).Item(j)) If j <> dt.Columns.Count - 1 Then writer.Write(delimiter) Next if i <> dt.Rows.Count - 1 then writer.Write(ControlChars.NewLine) Next writer.Flush() writer.Close() writer = Nothing Catch ex As Exception If Not writer Is Nothing Then writer = Nothing End If End Try
End Sub