STRANGE SqlDataReader Behavior

  • Thread starter Thread starter David Jessee
  • Start date Start date
D

David Jessee

I have a SqlDataReader.....

dim dr as SqlDataReader = GetReader;

I want to count the returned records....
dim i as integer
do while dr.read()
i+=1
Loop

I end up 2450 (life is good)

However, if I, instead say
dim i as integer
Dim en ad IEnumerator = DirectCast(dr,IEnumerator)
Do while en.MoveNext()
i+=1
Loop

I end up with 2394

What is up with that? Same SQL statement....no changes in the database.
Its just that when I cast the DataReader into an IEnumerable interface, its
not getting some records

Ideas?
 
Hi David,

Are you certain that you look at the same data?
 
One problem might be that if you do a Read to see if there are rows and then
bind to IEnumerator, it might be doing an initial Read--thus throwing away
one row. But that would not account for several missing rows...

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com/blog/billva
www.betav.com
www.sqlreportingservices.net
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
Isnt' that strange.
IEnumerator thing should do exactly the same...
David: Can you create a sample?
 
I'm very sure of it. That was my first thing to check

when read using the IDataReader Interface...I always get the correct number,
verified, by doing a select count(*)
(2450)
when I go through the IEnumerator interface I get a different number 2349,
2433, et (yes, it changes).
 
David,

I tried:

Dim i As Integer
Do While dr.Read
i = i + 1
Loop
MsgBox("Count = " & i)

and

Dim en As IEnumerator = DirectCast(dr, IEnumerable).GetEnumerator
Dim i As Integer
Do While en.MoveNext()
i = i + 1
Loop
MsgBox("Count = " & i)

I got the same result both times on a table with 25 rows.

Did you try both techniques on more than 1 table? Do you get different
results from the 2 techniques on every table?

Kerry Moorman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top