Reuse of datareader

  • Thread starter Thread starter Vanny
  • Start date Start date
V

Vanny

I tried to loop through the datareader to find some data elements one at the
time. The first time loop , it's fine. To avoid going to the database, I
reuse the same datareader to search for the next element. However, the 2nd
search starts from the place where I exit the loop, which causes the search
failed if the searched element is located before the first one. The
question is how could I make the loop start from row one again by using the
same datareader. Here how the code looks like for the search (strTempline is
the searched element which changes the value).

With drObject
If .HasRows Then
While .Read
If.Item("ref_id").ToString = strTempLine Then
strTableName = .Item("related_object").ToString
strTblRefId = strTempLine
blnExistRefid = True
Exit While
Else
blnExistRefid = False
End If
End While
End If
End With
Thanks for help

Vanny
 
This is easy. Load a DataTable with the DataReader stream.

Dt.Load dr

--
__________________________________________________________________________
William R. Vaughn
President and Founder Beta V Corporation
Author, Mentor, Dad, Grandpa
Microsoft MVP
(425) 556-9205 (Pacific time)
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
____________________________________________________________________________________________
 
Thanks for the response. That means, I have to go to the database, get the
same datareader and pass through the loop again in order to look for another
element?
Does vb.net have another quick way to find data like recordset.find in vb6.

Thanks,
Vanny
 
I don't see how this load to the datatable relates to the search. Could you
please explain more?

Thanks,
Vanny
 
Vanny,

William is trying to say, create a datatable and you can load your
datareader into
datatable.

Dim dt as DataTable = New DataTable("MyTable")
dt.Load(your datareader here)

and then you can work with datatable.

Thanks,
Sachin Saki
 
Vanny,

William is trying to say, create a datatable and you can load your
datareader into
datatable.

Dim dt as DataTable = New DataTable("MyTable")
dt.Load(your datareader here)

and then you can work with datatable.

Thanks,
Sachin Saki
 
The DataTable has a DefaultView (DataView) that can be searched, filtered
and sorted. You can also use Linq to manage it as well.


--
__________________________________________________________________________
William R. Vaughn
President and Founder Beta V Corporation
Author, Mentor, Dad, Grandpa
Microsoft MVP
(425) 556-9205 (Pacific time)
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
____________________________________________________________________________________________
 
Vanny,

You can use a datatable, that is almost the same as a recordset with the
only difference that it is disconnected.

dim dt as new datatable
dim con as new SqlConnection(connectionstring) 'the same as you use now
already
dim da as new SQLDataAdapter("the Sqltext",conn)
da.Fill(dt)

To go through that table by instance to get the first row
(2002/5005 style)
dim dr as datarow = dt.rows(0)
The last row
dim dr as datarow = dr.Rows(dt.rows.lenght -1)

To go to it through the next

for each dr as datarow in dt
dr............
next

To get the first item in the first row

dim firstItem as object = dt.rows(0).items(0) ' the latter as well with the
fieldnames as you use now.

Cor
 
Back
Top