问题
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!
回答1:
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.
回答2:
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?
来源:https://stackoverflow.com/questions/12463993/sqldatareader-does-not-return-all-records