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!
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
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
Getting the file from a mapped drive and putting the retrieved data in a dataset:
Dim folder = "Z:\"
Dim CnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & folder & ";Extended Properties=""text;HDR=No;FMT=Delimited"";"
Dim dssample As New DataSet
Using Adp As New OleDbDataAdapter("select * from [samplecsv.csv]", CnStr)
Adp.Fill(dssample)
End Using
If dssample.Tables.Count > 0 Then
'some code here
End If
Also, don't forget to include the
Imports System.Data.OleDb
And if you wish to link to a DataGridView (after read):
Dim bs As New BindingSource
bs.DataSource = dt
DataGridView1.DataSource = bs