DataReader to Array

  • Thread starter Thread starter jimmy
  • Start date Start date
J

jimmy

Hi,

Does anyone know how to read the contents of a datareader into an
array? The datareader will only hold one field called CuttingID. Can
anyone help me including how i would declare the array etc... I have
the

While dr.Read()

End While

part but don't know how to go about populating the array. Thanks in
advance

James
 
Does it have to be an Array or could you use a List <Of Whatever>? If
you use a List you just need to use it's .Add method to insert the
data.

Anyways, here's some air code for the array:

Dim str(1) as String

while dr.Read()
str(str.Length - 1) = CStr(dr.GetValue(0))
Redim str(str.Length)
end while

That should be close to what you need to do to use an array.

Thanks,

Seth Rowe
 
jimmy said:
Hi,

Does anyone know how to read the contents of a datareader into an
array? The datareader will only hold one field called CuttingID. Can
anyone help me including how i would declare the array etc... I have
the

While dr.Read()

End While

part but don't know how to go about populating the array. Thanks in
advance

VB 2003:

Dim values As New ArrayList
Dim CuttingIDs As Integer()

Dim dr As Data.OleDb.OleDbDataReader

While dr.Read()
values.Add(dr(0))
End While

CuttingIDs = DirectCast(values.ToArray(GetType(Integer)), Integer())



VB 2005:
Dim CuttingIDs As New System.Collections.Generic.List(Of Integer)

Dim dr As Data.OleDb.OleDbDataReader

While dr.Read()
CuttingIDs.Add(dr.GetInt32(0))
End While



Armin
 
Thanks alot to everyone who contributed to this. Managed to fix my
problem using a mixture of these ideas and also learnt something in the
process!
 
Seth,

I think that you would not give your solution without telling what is
happening in that.

For the OP there is everytime build a complete new array with the Redim, it
is not just setting of some pointers. Therefore the other showed arraylist
sample is much better.

Cor
 
I think that you would not give your solution without telling what is
happening in that.

Good point Cor, next time I won't be as lazy and explain things a
little :-)

Thanks,

Seth Rowe
 
Back
Top