SqlDataReader does not return all records

倾然丶 夕夏残阳落幕 提交于 2019-12-25 05:22:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!