Why use the GetOrdinal() Method of the SqlDataReader

前端 未结 4 2047
北海茫月
北海茫月 2020-12-16 12:55

What\'s the difference between reading a value from an SqlDataReader using this syntax:

Dim reader As SqlClient.SqlDataReader
reader(\"value\").ToString()
         


        
4条回答
  •  攒了一身酷
    2020-12-16 13:14

    I just want to add that the context of how many records you are expecting plays a big role because if you are returning a single row then performance difference between those two would not be significant. However if you are looping over many rows then using typed accessor is better for performance since it's optimized. So in that case if you need to get best performance by using a column name then call GetOrdinal once, put it into a variable and then use typed accessor with the column ordinal in your loop. This would yield the best performance.

    if you are curious about the performance difference check out my blog post

提交回复
热议问题