当字段不存在时,这会引发异常:
reader.IsDbNull(reader.GetOrdinal("FieldName"))
=> 砰
为什么不返回 -1 ?
当字段不存在时,这会引发异常:
reader.IsDbNull(reader.GetOrdinal("FieldName"))
=> 砰
为什么不返回 -1 ?
I'll try to guess here.
The common pattern for this method is to call GetOrdinal for column name and then call GetXXX() methods with given ordinal which is faster than do a search by column's name every time.
Therefore in case of exception here we fail fast and we can't ignore it. Wihtout exception we will try to find a column that doesn't exist and then try to find a field by given ordinal (without checking for -1 which is very easy to omit in this case) and only here we will realise that something went wrong few steps before (may be even too many steps before).