locked
Convert object to timespan RRS feed

  • Question

  • What is the prefered method?
    Should I just simple cast?

    TimeSpan endTime = (TimeSpan)reader["END_TIME"];

    I see that a data reader also has a method "GetTimeSpan", but it only allows you to use a numerical index, when I'd rather use a column name. Column indexes change to much and aren't as clear.

    Tuesday, February 16, 2010 7:33 PM

Answers

  • There's no single answer to this question.  If casting works in this situation, then use that.  Be sure to check to make sure that reader["END_TIME"] doesn't equal DBNull.Value, however, indicating that the value from the database is null.  If it does equal null, you're going to get a nasty InvalidCastException. 

    You can also use GetTimeSpan, but simply use GetOrdinal with it. 

    TimeSpan endTime = reader.GetTimeSpan(reader.GetOrdinal("END_TIME"));


    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
    Tuesday, February 16, 2010 7:43 PM