Solved

Visual basic reading csv file into array

Posted on 2010-11-28
4
732 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 8

Expert Comment

by:pdd1lan
ID: 34225174
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
ID: 34225261
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
ID: 34230286
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
ID: 34233408
You already have the data in a datatable? Is there a particular reason to populate an array?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

717 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