Solved

Visual basic reading csv file into array

Posted on 2010-11-28
4
712 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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