How to jump into N record with sqldatareader

yugicm

New member
Joined
Oct 30, 2009
Messages
1
Programming Experience
5-10
Hi,

suppose i have syntax like this:

Dim cn As New SqlConnection(ConString)
Dim cmd As New SqlCommand("select * from emp", cn)
Dim dr As SqlDataReader
cn.Open()
dr = cmd.ExecuteReader

suppose that the table emp have 500 record. now i just want to view record number 320. how am i do this? i usually do this:

if dr.hasrows then
dim i as integer=0
while dr.read
i += 1
if i = 320 then
txtname.text=dr.item("name")
txtaddress.text=dr.item("address")
end if
end while
end if

is there any efective way to do this? please help. thanks

yugi
 
What you're asking for is not possible. DataReaders provide read-only, forward-only access to the result set of a query. That means that, to read a particular record, you have to have already read every record before it.

If you don't need all the data then I suggest that you tailor your query to not return all the data.

If you need random access to the data then you need to populate a DataTable. The DataTable.Load method will read an entire result from a DataReader into the DataTable, so you can do that with your existing DataReader. Alternatively you can use a DataAdapter.
 
Back
Top