none
SqlDataReader.GetXXX([ColumnOrdinal]) vs Convert.XXX(SqlDataReader["columnname"]) RRS feed

  • Question

  •  

     

    A friend and I got into a heated discussion whether it is better to use sqldatareader.GetXXX or to do an explicit conversion Covert.XXX, and we also wondered why Microsoft hasn't provided a sqldatareader.GetXXX([columnname]).

     

    According to MS documentation in the Remarks section for GetInt32, "

    No conversions are performed; therefore, the data retrieved must already be a 32-bit signed integer.".  Where as in the latter there is an explicit conversion.

     

    Looking for any feed back on performance hits, reasoning, pros, cons. 

     

    TIA

     

    Brian

    Thursday, July 12, 2007 1:52 PM

Answers

  • I'm not sure if performance is really an issue with either methodology.  Calling GetInt32 calls the same code as the indexer, but applies a cast.  The only benefit that I have found, one way or the other, is to use the indexer and Convert when using business objects as my data layer.  Sometimes the structure of your business object doesn't necessarily match the schema used in your table (e.g. sometimes boolean values are stored as ints in the table).
    Thursday, July 12, 2007 8:29 PM

All replies

  •  

    OK

     

    I ran both methods through the ildasm.exe and this is what was produced.

     

    1> DataReader GetInt32([column ordinal]) Methods

    Code Snippet

    IL_0067: ldloc.3

    IL_0068: ldloc.3

    IL_0069: ldstr "ID"

    IL_006e: callvirt instance int32 [System.Data]System.Data.Common.DbDataReader::GetOrdinal(string)

    IL_0073: callvirt instance int32 [System.Data]System.Data.Common.DbDataReader::GetInt32(int32)

    IL_0078: stloc.s id

     

     

    2>Convert Methods

    Code Snippet

    IL_0068: ldloc.3

    IL_0069: ldstr "ID"

    IL_006e: callvirt instance object [System.Data]System.Data.Common.DbDataReader::get_Item(string)

    IL_0073: call int32 [mscorlib]System.Convert::ToInt32(object)

    IL_0078: stloc.s id

     

     

    I'm not really experience with MSIL.  I see an additional PUSH(ldloc.3)  for the getordinal in method 1.  However method 2 still has to deal with types other then the intended int32.  Anyone who is well versed in this please feel free to chime in. 

     

    I did want to note, if there was a sqldatareader.GetXXX["column name"], method 1 would require one less PUSH and CALLVIRT instruction which would make it a tiny bit faster.  Which leads back to why is there a method for this???? 

     

    Brian

    Thursday, July 12, 2007 7:33 PM
  • I'm not sure if performance is really an issue with either methodology.  Calling GetInt32 calls the same code as the indexer, but applies a cast.  The only benefit that I have found, one way or the other, is to use the indexer and Convert when using business objects as my data layer.  Sometimes the structure of your business object doesn't necessarily match the schema used in your table (e.g. sometimes boolean values are stored as ints in the table).
    Thursday, July 12, 2007 8:29 PM