Create a CSV file from Dataset

First Declare a form level Dataset

Public Class frmCSV
    Dim ds As New DataSet

Next connect to your data source and build your dataset. In this case I am going to build one on the fly with this

Private Sub BuildDS()
     Dim dt As New DataTable
     Dim dr As DataRow
     Dim idColumn As DataColumn = _
         New DataColumn("ID", Type.GetType("System.Int32"))
     Dim FNColumn As DataColumn = _
         New DataColumn("FirstName", Type.GetType("System.String"))
     Dim LNColumn As DataColumn = _
         New DataColumn("LastName", Type.GetType("System.String"))

     dt.Columns.Add(idColumn)
     dt.Columns.Add(FNColumn)
     dt.Columns.Add(LNColumn)

     dr = dt.NewRow()
     dr("ID") = 1
     dr("FirstName") = "Randy"
     dr("LastName") = "Flag"
     dt.Rows.Add(dr)

     dr = dt.NewRow()
     dr("ID") = 2
     dr("FirstName") = "Tom"
     dr("LastName") = "Servo"
     dt.Rows.Add(dr)

     ds.Tables.Add(dt)

 End Sub

Now the code to loop trough your dataset and write the results to a file.

Private Sub WriteDS2CSV(ByVal ds As DataSet, ByVal FilePath As String)
    Dim str As New System.Text.StringBuilder
    For Each dr As DataRow In ds.Tables(0).Rows
        For Each field As Object In dr.ItemArray
            str.Append(field.ToString & ",")
        Next
        str.Replace(",", vbNewLine, str.Length - 1, 1)
    Next
    Try
        My.Computer.FileSystem.WriteAllText(FilePath, str.ToString, False)
        MessageBox.Show("File Created", "Success", MessageBoxButtons.OK)
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Write Error", MessageBoxButtons.OK)
    End Try
End Sub

All that is left is to actually build the dataset and then pass it as well as the file name and path to the WriteDS2CSV sub.

 

Private Sub btnWriteCSV_Click( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnWriteCSV.Click

    BuildDS()
    WriteDS2CSV(ds, "C:\dsExport.csv")

End Sub

Share and Enjoy:
  • Print
  • Facebook
  • Google Bookmarks

Comments are closed.