Solved

Visual basic reading csv file into array

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb.net 2 37
Class object 2 27
Advice on Xojo as a development tool over VB. 4 39
Run a batch file when a Crystal Report is opened 5 15
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

14 Experts available now in Live!

Get 1:1 Help Now