DataReader - hardcode ordinals?

前端 未结 6 2019
执念已碎
执念已碎 2021-01-11 10:29

When returning data from a DataReader I would typically use the ordinal reference on the DataReader to grab the relevant column:

if         


        
6条回答
  •  星月不相逢
    2021-01-11 10:58

    Indexing the data reader by name is slightly more expensive. There are two main reasons for this.

    • Typical implementations store field information in a data structure that uses numerical indexing. A mapping operation has to take place to transpose a name into a number.
    • Some implementations will do a two-pass lookup on the name. The first pass tries to match field names with case-sensitivity turned on. If that pass fails then a second pass begins with case-sensitivity turned off.

    However, in most cases the performance penalty incurred by looking up a field by name is dwarfed by the amount of time it takes for the database to execute the command. Do not let the performance penalty dictate your choice between name and numeric indexing.

    Despite the slight performance penalty I normally choose name indexing for two reasons.

    • The code is easier to read.
    • The code is more tolerant to changes in the schema of the resultset.

    If you feel like the performance penalty of name indexing is becoming problematic (maybe the command executes quickly, but returns a lot of rows) then lookup the numeric index once by name, save it away, and use it for the remaining rows.

提交回复
热议问题