Loop Question

SIMIN

Well-known member
Joined
Mar 10, 2008
Messages
92
Hello,
I use this code to loop through all items and read all of them, from 1st item to the last one!
Code:
Dim ReadUsers As New OleDb.OleDbCommand("SELECT * FROM Emails WHERE GroupID = " + MyID + "" + SortSQL, MyConnections)
Dim MyReader1 As OleDbDataReader = ReadUsers.ExecuteReader
While MyReader1.Read
    ...
End While
This is OK when I want to read all items of this table.
But what should I do if I want to read only item number 100 to 200?
I mean in the above code how can I change the While loop to a For loop with specified start and end point?
Thank you for helping :)
 
DataReaders are designed to be accessed in a sequential manner, there is no way to skip to a certain row without reading all the rows before it.

Personally I would change the select statement to just bring back the rows I am interested in rather than all rows. You might alslo want to look at parameterising the query as building a string that way can open you up to security risks.
 
Thanks, you know theres a problem, my real database has around 10.000 records.
When I want to read all of records, theres no problem.
But for example, if I want to read the last 1000 records, from 9001 to 10.000, there will be a long delay if I want to read the firs 9.000 records and just skip them / continue while...
What other way do you recommend?
 
User specify that for example he wants to read from record #1 to #100
OR from #1000 to #1500
 
Do these numbers match up with a particular field or would they simply be based on the data returned?

Out of interest what database are you using?
 
Back
Top