bang Why not return -1 ?" /> bang Why not return -1 ?" /> bang Why not return -1 ?"/>

Why does reader.GetOrdinal("FieldName") throw an exception?

2.3k Views Asked by At

this throws an exception, when the field does not exist:

reader.IsDbNull(reader.GetOrdinal("FieldName")) => bang

Why not return -1 ?

1

There are 1 best solutions below

0
Andrey Taptunov On

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).