Imports System.Data.OleDb


Module Module1


    Sub Main()
        Dim Cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\mibase.mdb;Persist Security Info=False")
        Cn.Open()
        Dim comando As OleDbCommand
        comando = New OleDbCommand()
        comando.CommandText = String.Format("SELECT Telefono FROM empleados ")

        comando.Connection = Cn
        Dim lector As OleDbDataReader
        lector = comando.ExecuteReader


        Do While lector.Read

            Console.WriteLine(lector("Telefono"))

        Loop

       
        Dim nombres As New ArrayList




            While lector.Read
            nombres.Add(lector("Telefono"))

            Console.WriteLine(nombres.ToString)

            End While

        For Each datos As String In nombres
            Console.WriteLine(datos)


        Next


    End Sub

End Module

I have this..but is not working I cant save data in the array and the array is not printing is like empty

Dani AI

Generated

Nice catch by — the symptoms in the original post point to two different, common mistakes: iterating a forward-only DataReader twice, and expecting a collection’s ToString to show its contents. A DataReader is read-only and forward-only; once it reaches the end it cannot be rewound, so results must be captured while doing the first read (or the command must be re-executed). Also, calling SomeCollection.ToString() returns the type name (for example, "System.Collections.ArrayList"), not the items inside it.

A small, safer pattern that avoids those pitfalls:

  • use a generic List(Of String) instead of ArrayList for type safety;
  • read the DataReader once and store values into the list while checking for DBNull;
  • use Using blocks so connection/command/reader are properly disposed.

Example (adapt the connection string and column name as needed):

Using cn As New OleDbConnection(connectionString)
    cn.Open()
    Using cmd As New OleDbCommand("SELECT Telefono FROM empleados", cn)
        Using rdr As OleDbDataReader = cmd.ExecuteReader()
            Dim results As New List(Of String)
            While rdr.Read()
                Dim val As String = If(rdr.IsDBNull(0), String.Empty, Convert.ToString(rdr(0)))
                results.Add(val)
            End While
        End Using
    End Using
End Using

For Each phone As String In results
    Console.WriteLine(phone)
Next

Quick notes: compile the project as x86 if using the Jet OLEDB 4.0 provider on modern (64-bit) machines, or switch to the ACE provider or a server-side DB if required. If re-reading the same query is needed, re-run ExecuteReader (or cache results) rather than trying to rewind the reader.

SOLVED =) ...like 1 hour but my mistake was in for each...

Imports System.Data.OleDb


Module Module1


    Sub Main()
        Dim Cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\mibase.mdb;Persist Security Info=False")
        Cn.Open()
        Dim comando As OleDbCommand
        comando = New OleDbCommand()
        comando.CommandText = String.Format("SELECT Nombre FROM empleados ")

        comando.Connection = Cn
        Dim lector As OleDbDataReader


        lector = comando.ExecuteReader
        Dim x As Integer = 0


        Dim milista As New ArrayList

        While lector.Read
            milista.Add(lector.GetString(0))
            x = x + 1
        

        End While
        For Each dato As String In milista
            Console.WriteLine(dato)


        Next


    End Sub

End Module
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.