Solved

Visual basic reading csv file into array

Posted on 2010-11-28
4
678 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:sykotex
4 Comments
 
LVL 8

Expert Comment

by:pdd1lan
Comment Utility
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
0
 
LVL 8

Expert Comment

by:Mohit Vijay
Comment Utility
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
0
 
LVL 2

Accepted Solution

by:
dme3 earned 500 total points
Comment Utility
You could either read the code into an ArrayList or a List<String> (using generics) and then convert he List to an array, or you could read it straight into an array like so:


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)

        Dim myArray(dt.Rows.Count) As String

        If dt IsNot Nothing Then
            For i As Integer = 0 To myArray.Length - 1

                'add into Array

                myArray(i) = (dt.Rows(i)("Host name").ToString() & ", " & dt.Rows(i)("machine admin name").ToString())

                'display
                MsgBox(myArray(i))
            Next
        End If


0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
You already have the data in a datatable? Is there a particular reason to populate an array?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now