Murray Brown
asked on
VB.net Create a data table from a text file
Hi
I am trying to create a DataTable from a text file such as the one attached .
The code seems to run fine but the data table seems to be blank
I am trying to create a DataTable from a text file such as the one attached .
The code seems to run fine but the data table seems to be blank
Sub Create_DataTable
Dim dt As DataTable = GetDataTableFromCsv(sFileAndPath, False)
End Sub
Private Shared Function GetDataTableFromCsv(path__1 As String, isFirstRowHeader As Boolean) As DataTable
Dim header As String = If(isFirstRowHeader, "Yes", "No")
Dim pathOnly As String = Path.GetDirectoryName(path__1)
Dim fileName As String = Path.GetFileName(path__1)
Dim sql As String = "SELECT * FROM [" & fileName & "]"
Using connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & pathOnly & ";Extended Properties=""Text;HDR=" & header & """")
Using command As New OleDbCommand(sql, connection)
Using adapter As New OleDbDataAdapter(command)
Dim dataTable As New DataTable()
dataTable.Locale = CultureInfo.CurrentCulture
adapter.Fill(dataTable)
Return dataTable
End Using
End Using
End Using
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER