Arraylist from SQLReader

I want to populate an arraylist from an SQLDataReader so I can pass it back up to another object and extract the various columns.

            While oServerReader.Read
                rtnArray.Add(Trim(oServerReader.GetString(0)))
            End While

That's not right.




Answer this question

Arraylist from SQLReader

  • Castro

     John Chen MS wrote:
    Hi Dave,

    You question seems nothing to do with ArrayList to me.
    What exactly the error you have seen
    You may want to debug it by print out the result from DataReader like:
               While oServerReader.Read
                    ' try print out oServerReader.GetString(0)
                End While


    Thanks!

    JohnChen

    rtnArray is an arraylist I've dimmed. Now I want to add each line from the reader to it, so that what I pass back is an arraylist where each item is a row from the reader.

    I'll then want to know how to access the various entries in each item on the arraylist.

  • Noorali

    well I think the point paul was making is, why are you using an arraylist when a datatable is available to you.

    why rebuild the wheel

    now if you are absolutely adamant about returning an ArrayList where each item in the ArrayList is represents a row from the reader as an array of values, the easiest way of doing it is. . .


    dim sql as string = "select . . . "
    dim da as SQLDataAdapter = new SQLDataAdapter(sql, someConnection)
    dim dt as datatable = new DataTable()
    da.Fill(dt)
    foreach row as DataRow in dt.Rows
       rtnArray.Add(row.ItemArray)
    next row

     


    but again, that would be foolish. just use the datatable.
    Why copy data from a perfectly adequate structure to an inadequate structure

    is it because you need to pass the rtnArray to another method that takes an array list as an argument

    Well, that's the design error!!!
    Don't declare your methods with Class type parameters, declare them with interfaces!!!

    The method should not take an ArrayList, but an interface - ICollection or IList.

    for example, Never declare this method:

    sub DoSomething(byval MyList as ArrayList)
     

    It is plain wrong!!!
    Declare this method:

    sub DoSomething(byval MyList as IList)
     


    Fill a Dataset, and pass the DefaultView to the method or if you want, pass an ArrayList or any other class that implements ICollection/IList.

    note: my vb may have typos as I rarely use the inferior language

  • OCCcoach

    The question orignal seemed easy,

    but the answer later became more and more complicated!

     cindyding0412 wrote:
    But you are all professional,

    Been called many things. . . rarely get that one Big Smile !!!

     cindyding0412 wrote:
    so want more information
    Here comes it for you, see my sig...Big Smile

    What am I looking for
    Get a recipe for Kung-pao Chicken
     cindyding0412 wrote:
    May you like it! 

    Your picture Yeah, you are a cutie ;)

    Um whats with the copyrighted ADO book pdf on your site, eh

  • Barrie A Kenyon

    It seems that 'beating someone over the head in admonishment' is the real use for the hammer. You didn't need to be quite so verbose.Sad



  • remster

    Hi,

    I think you could solve this if you have a temporary storage for each of your row in the datareader. You can achieve this by creating a class/struct that corresponds to the fields in your row:

    Structure test
        Public id As Integer
        Public name As String
    End Structure

    '' On your read loop
    while oServerReader.Read
    Dim row As test
    row.id = oServerReader("Id")
    Arraylist.add(row)
    End While

    BTW, you can use a class instead of a structure...

     

    cheers,

    Paul June A. Domag



  • zeeshan_ahmad

    Hi Dave,

    You question seems nothing to do with ArrayList to me.
    What exactly the error you have seen
    You may want to debug it by print out the result from DataReader like:
               While oServerReader.Read
                    ' try print out oServerReader.GetString(0)
                End While


    Thanks!

    JohnChen

  • LonW

    this:

    dim sql as string = "select . . . "
    dim da as SQLDataAdapter = new SQLDataAdapter(sql, someConnection)
    dim dt as datatable = new DataTable()
    da.Fill(dt)
    foreach row as DataRow in dt.Rows
       rtnArray.Add(row.ItemArray)
    next row

     


    could also be done as:


    While rdr.Read
       Dim arr(rdr.FieldCount) As Object
       rdr.GetValues(arr)
       rtnArray.Add(arr)
    End While

     


  • Paulyz

    Hi Interesting dialog. I think the reason why Dave wants to populate it into array is because he cant do a UNION for a list of data read from the database.

    How do you make a UNION from the data read from a cursor You cant. So populate it into array and later display it a table.

    Do you have an idea how

  • roadragedsb

     Paul Domag wrote:
    You can just create an adapter and fill a datatable. I think this approach is much convenient. Coz its quite the same as traversing the whole reader and placing it in an array list...

    Thanks, but I'm looking to fill an arraylist, not a datatable.


  • AndresF

    Hi,

    You can just create an adapter and fill a datatable. I think this approach is much convenient. Coz its quite the same as traversing the whole reader and placing it in an array list...

     

     

    cheers,

    Paul June A. Domag



  • Washington

    I hope the following code will be of great help to you






    While oServerReader.Read
    Dim Tmp As System.Collections.ArrayList = New System.Collections.ArrayList
    Dim lintCnt As Integer = 0
    While lintCnt < oServerReader.FieldCount
    Tmp.Add(oServerReader(0))
    System.Math.Min(System.Threading.Interlocked.Increment(lintCnt),lintCnt-1)
    End While
    rtnArray.Add(Tmp)
    End While


     



  • Arno Nel

    SadIdea
    The question orignal seemed easy,

    but the answer later became more and more complicated!

    But you are all professional,

    so want more information

    Here comes it for you, see my sig...Big Smile

    May you like it! 

  • Sunil Pawar

    Apprentice Carpenter: "Master, Can you show me how to drive screws with this hammer "

    Master Carpenter: "Don't drive screws with a hammer, use a screwdriver!"

    Apprentice Carpenter: "But Master, I have a hammer in my hand so I have to use the hammer!"

    Master Carpenter: "Well, put it down and pick up a screwdriver!"

    Apprentice Carpenter: "But Master, I only know how to use hammers!"

    Master Carpenter: "Well, obviously you don't because you are trying to drive screws with it!"

    Apprentice Carpenter: "Master, you are a bad master, because you won't train me to drive screws with a hammer!"

    Master Carpenter: "I think you should find another line of work as you lack the apptitude and attidude neccesary to become a master carpenter."

  • kittensattack

    Right. Yes. I was missing a whole chunk where I take the row and convert it into my class before storing it in the arraylist.

  • Arraylist from SQLReader