Why use the GetOrdinal() Method of the SqlDataReader

前端 未结 4 2045
北海茫月
北海茫月 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:36

    I think that the reason to use GetOrdinal() is so that you can cache the result and re-use it multiple times for performance.

    E.g.

    Dim reader As SqlClient.SqlDataReader
    int valueOrdinal = reader.GetOrdinal("value");
    while ( ... )
    {
        var value = reader.GetString(valueOrdinal);
    }
    

提交回复
热议问题