Try this function... It'll read the csv file and return a datatable
vb Code:
Private Function CsvToDataTable(ByVal filePath As String) As DataTable Dim dt As DataTable = Nothing Dim sourcePath As String = String.Empty Dim csvFile As String = String.Empty Dim conString As String = String.Empty Dim conn As OleDb.OleDbConnection = Nothing Dim adapter As OleDb.OleDbDataAdapter = Nothing Dim selString As String = String.Empty Try sourcePath = System.IO.Path.GetDirectoryName(filePath) csvFile = System.IO.Path.GetFileName(filePath) conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sourcePath & ";Extended Properties=""text;HDR=No;FMT=Delimited"";" conn = New OleDb.OleDbConnection(conString) selString = "Select * From " & csvFile adapter = New OleDb.OleDbDataAdapter(selString, conn) dt = New DataTable(System.IO.Path.GetFileNameWithoutExtension(filePath)) conn.Open() adapter.Fill(dt) conn.Close() Catch ex As Exception MessageBox.Show(ex.Message) Finally adapter.Dispose() conn.Dispose() End Try Return dt End Function




Reply With Quote