0

I am experiencing a problem with ADO.NET SqlDataReader. When I run underlying stored procedure directly in SSMS - it returns 1.7 million of records. When I run a related VB.NET code that fills an ADO.NET DataTable - I also get 1.7 million of records.

But when I run a loop to fill a Generic list like this

While i_oDataReader.Read
   m_aFullIDList.Add(i_oDataReader.GetInt32(0))
End While

It returns a lot less records and that number can vary. At the end of the loop if I check m_aFullIDList.Count it ca be 100000 or 500000 etc. Any idea why and how to fix it? Thanks!

3
  • 1
    Do you get an exception, maybe a timeout? Commented Sep 17, 2012 at 17:07
  • No, the loops runs thru without any errors, just the count at the end is wrong. It's sounds silly but it's almost as if SQL Server cannot catch up with DataReader and the loop is finished before full result is returned. Commented Sep 17, 2012 at 17:12
  • But actually you reference pointed me to the right direction. Adding "CommandBehavior.SequentialAccess" while creating the DataReader fixed the issue! Thanks :) Commented Sep 17, 2012 at 17:21

2 Answers 2

2

Thanks to reference pointed by @Tim Schemlter I found the option "CommandBehavior.SequentialAccess" for DataReader creation. That fixed the issue. E.g. instead of

drReader = oCommand.ExecuteReader();

use

drReader = oCommand.ExecuteReader(CommandBehavior.SequentialAccess);

and it works correctly.

Sign up to request clarification or add additional context in comments.

2 Comments

No, not a blob, plain Int, but a whole lot of those.
And a funny thing - it takes about 14 seconds to execute query directly in SSMS, but only about a second to execute a loop above.
0

Have you tried using the GetInt64() method instead? I realize that with only 1.7 million records, the GetInt32() method should be large enough; I was more just curious.

Also, if you think that SQL cannot keep up with the DataReader, have you tried adding a wait in the loop to allow it to catch up?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.