store database multiple records into variables using a DataReader

302 Views Asked by At

I'm trying to retrieve and store 14 database records into variables using a DataReader(). I know how to store multiple fields into variables, but I don't know how to store 14 records with one column. I'm using MS Access and VB.

Try
    con.Open()
    dr = cmd.ExecuteReader()
    While dr.Read
        variableName = dr.Item("Description")
        Now, how can I do it for the other 13 variables?????
    End While
    con.Close()
Catch ex As Exception
    con.Close()
    MsgBox(ex.Message) : Exit Sub
End Try
1

There are 1 best solutions below

1
On

One thing you can try, is create a List variable, and then using a for each look, cycle through each data row, and add the column to the list variable. The code is not exact but should be enough information to get you along.

Dim lst as new List(of String)

    Try
        con.Open()
        dr = cmd.ExecuteReader()
        While dr.Read
         For Each rw as datarow in dr.Rows()
            lst.add(rw.item("Description"))
          Next
        End While
        con.Close()
    Catch ex As Exception
        con.Close()
        MsgBox(ex.Message) : Exit Sub
    End Try