DataReader rows into a collection object

Hi,

I have a datreader object with 100 rows, i created a collection object[ arraylist / hashtable] and stored all the rows to the collection object.

I dont want to keep the connection opened that's the reason i stored the data to the collection object and killed the datareader object.

But the problem is I wanted to retrieve the value using column names not by index. For this I looped thru the column names from datareader, how do I merge these two objects?

Any help is highly appreciated.

[500 byte] By [MaheshChandramouli] at [2007-12-17]
# 1
Why not just use a DataTable? The following link works well if you're using VS.NET 2003 or before ...
http://codebetter.com/blogs/grant.killian/archive/2004/03/23/9819.aspx

Or you could just run the SQL to populate the DataTable directly. I find this much easier ...

Josh

JoshLindenmuth at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 2
Hi Josh,

We don't use datatables in our application, we use custom objects.

MaheshChandramouli at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 3
Now I'm confused ... If you're not using databases, why use DataReaders at all?
JoshLindenmuth at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 4

The datareader gets the data....the datatables persist the data in structured memory...you can also utilize custom classes/objects (business entities) to do the same thing as a datatable...

<quote>But the problem is I wanted to retrieve the value using column names not by index. For this I looped thru the column names from datareader, how do I merge these two objects?</>


While MyReader.Read
For x As Integer = 0 To Myreader.FieldCount - 1

MyObject.FieldArray(x) = Myreader(x)

Next
loop

If you want to do this with column names then I would suggest using an array of names also....something like this:



While MyReader.Read
For x As Integer = 0 To Myreader.FieldCount - 1

MyObject.FieldArray(x) = Myreader(MyColumnNames(x))

Next
loop


DMan1 at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...

.NET Development

Site Classified