Link to home
Start Free TrialLog in
Avatar of sykotex
sykotexFlag for United Kingdom of Great Britain and Northern Ireland

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

Open in new window

Avatar of pdd1lan
pdd1lan

Dim myArray as new ArrayList()

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
             
              '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
Dim myArrayList as new ArrayList()

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
             
              '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
Avatar of dme3
dme3

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nasir Razzaq
You already have the data in a datatable? Is there a particular reason to populate an array?