I am going to do my best to explain this, though admittedly I have not attempted much in months so I’m not only rusty, I wasn’t good to being with.
I am using visual web developers and asp, vb is the code behind with a sql db.
If I select a columns from a table, for example:
sqlCmd.CommandText = "SELECT Username, W1, W2, W3, W4 FROM tablename"
Say there are multiple rows in this table with data in these columns.
When I do a datareader, or how I have been shown, I declare dr like:
Dim dr As Data.SqlClient.SqlDataReader
I can work with the selected items such as:
dr.item(0)
dr.item(1)
etc.
But the only items I can work with are the first row of items selected. How do I select all of the rows in the table. Or how can I work with the data from multiple rows using dr.item or by somehow telling it to move onto the next row so that dr.item(0) becomes the username for the second row in the table.
I hope that made sense and I’m sorry if this is a stupid question. I appreciate the time and help in advance. Thanks guys.
SqlDataReader.Readadvances the reader to the next record and returnstruewhen there is at least one other row:Use
Usingto dispose anything that implementsIDisposableas soon as possible. It will also close connections implicitely.Edit: using a
DataTableThe
DataReaderapproach above works well, but if you want to select all rows anyway and it’s ok to load all into memory, you can use aDataTableinstead. Then you could also access each row via indexer like an array or list: