sykotex
asked on
Visual basic reading csv file into array
I now have code that connects to a csv file and can read it and display into a msgbox, what code do I add to read into an array?
Dim con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Environment.CurrentDirectory & ";" + "Extended Properties='text;FMT=Delimited(;);HDR=YES'")
Dim da As New OleDbDataAdapter("select * from [input.csv]", con)
Dim dt As New DataTable
da.Fill(dt)
If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
For Each row As DataRow In dt.Rows
'Read all
MsgBox(row("Host name").ToString() & ", " & row("machine admin name").ToString())
Next
End If
Dim myArrayList as new ArrayList()
Dim con As New OleDbConnection("Provider= Microsoft. Jet.OLEDB. 4.0;Data Source=" & Environment.CurrentDirecto ry & ";" + "Extended Properties='text;FMT=Delim ited(;);HD R=YES'")
Dim da As New OleDbDataAdapter("select * from [input.csv]", con)
Dim dt As New DataTable
da.Fill(dt)
If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
For Each row As DataRow In dt.Rows
'add into Array
myArrayList.Add(row("Host name").ToString() & ", " & row("machine admin name").ToString())
'Read all
MsgBox(row("Host name").ToString() & ", " & row("machine admin name").ToString())
Next
End If
Dim con As New OleDbConnection("Provider=
Dim da As New OleDbDataAdapter("select * from [input.csv]", con)
Dim dt As New DataTable
da.Fill(dt)
If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
For Each row As DataRow In dt.Rows
'add into Array
myArrayList.Add(row("Host name").ToString() & ", " & row("machine admin name").ToString())
'Read all
MsgBox(row("Host name").ToString() & ", " & row("machine admin name").ToString())
Next
End If
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You already have the data in a datatable? Is there a particular reason to populate an array?
Dim con As New OleDbConnection("Provider=
Dim da As New OleDbDataAdapter("select * from [input.csv]", con)
Dim dt As New DataTable
da.Fill(dt)
If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
For Each row As DataRow In dt.Rows
'add into Array
myArray.Add(row("Host name").ToString())
myArray.Add(row("machine admin name").ToString())
'Read all
MsgBox(row("Host name").ToString() & ", " & row("machine admin name").ToString())
Next
End If