How to get number of rows using SqlDataReader in C#

后端 未结 6 963
故里飘歌
故里飘歌 2020-11-28 09:18

My question is how to get the number of rows returned by a query using SqlDataReader in C#. I\'ve seen some answers about this but none were clearly defined exc

6条回答
  •  旧巷少年郎
    2020-11-28 10:11

    You can't get a count of rows directly from a data reader because it's what is known as a firehose cursor - which means that the data is read on a row by row basis based on the read being performed. I'd advise against doing 2 reads on the data because there's the potential that the data has changed between doing the 2 reads, and thus you'd get different results.

    What you could do is read the data into a temporary structure, and use that in place of the second read. Alternatively, you'll need to change the mechanism by which you retrieve the data and use something like a DataTable instead.

提交回复
热议问题