Should I call SqlDataReader.HasRows if I am calling SqlReader.Read

后端 未结 5 1838
一向
一向 2020-12-10 01:46

Trying to see if it is beneficial to add an if (dr.HasRows) before the while (dr.read()) function. I mean, technically if it doesn\'t have rows it

5条回答
  •  粉色の甜心
    2020-12-10 02:11

    No..It is not mandatory to check (dr.HasRows) if the DataReader contains any row or not.

    Read() will return False if there are no more rows to fetch, but Reader.HasRows is much more telling as to what it does than Read() so it would be a good practice to use Reader.HasRows because you may accidentally do something other than Read() which may fall into exception.

提交回复
热议问题