1 Previous Next 

CSV/Text to Datatable (OleDb)


Using this sample, you can make from a CSV file a dataset. Be aware that it uses the culture used delimiter what is not always a comma however can as well be a semicolon

This sample needs only a datagridvieew on a form and paste the code in the form class
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgsHandles MyBase.Load
        Dim file As String = "test.txt"
        Dim path As String = "C:\Test\"
        Dim ds As New DataSet
        Try
            If IO.File.Exists(IO.Path.Combine(path, file)) Then
                Dim ConStr As String = _
                "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
                path & ";Extended Properties=""Text;HDR=No;FMT=Delimited\"""
                Dim conn As New OleDb.OleDbConnection(ConStr)
                Dim da As New OleDb.OleDbDataAdapter("Select * from " & _
                file, conn)
                da.Fill(ds, "TextFile")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        DataGridView1.DataSource = ds.Tables(0)
    End Sub
End Class



1 Previous Next