Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert csv data to DataTable in VB.net

I am trying to import a large array of integers stored as a csv file into a VB.Net DataTable called BeamMap. The .csv file consists only of integers, with a delimiter of ,, no quotes around the data (ie., 1,3,-2,44,1), and an end of line character of line feed and carriage return. All I want to do is get each integer into a DataTable cell with the appropriate rows and columns (there are the same number of columns for each row) and be able to reference it later on in my code. I really don't want anything more than absolutely necessary in the code (no titles, captions, headings, etc.), and I need it to be fairly efficient (the csv array is approx. ~1000 x ~1000).

Thanks!

like image 841
Matt Avatar asked Jun 20 '12 11:06

Matt


2 Answers

Use OleDb provider to read CSV and pouplate the DataTable.

 Dim folder = "c:\location\of\csv\files\"
 Dim CnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & folder & ";Extended Properties=""text;HDR=No;FMT=Delimited"";"
 Dim dt As New DataTable
 Using Adp As New OleDbDataAdapter("select * from [nos.csv]", CnStr)
       Adp.Fill(dt)
 End Using
like image 139
KV Prajapati Avatar answered Sep 28 '22 08:09

KV Prajapati


Here's a simple approach which requires a strict format (as you've mentioned):

Dim lines = IO.File.ReadAllLines(path)
Dim tbl = New DataTable
Dim colCount = lines.First.Split(","c).Length
For i As Int32 = 1 To colCount
    tbl.Columns.Add(New DataColumn("Column_" & i, GetType(Int32)))
Next
For Each line In lines
    Dim objFields = From field In line.Split(","c)
                 Select CType(Int32.Parse(field), Object)
    Dim newRow = tbl.Rows.Add()
    newRow.ItemArray = objFields.ToArray()
Next
like image 40
Tim Schmelter Avatar answered Sep 28 '22 08:09

Tim Schmelter