SafeDataReader SQL2008 time(7) datatype, GetTimeSpan()

SafeDataReader SQL2008 time(7) datatype, GetTimeSpan()

Old forum URL: forums.lhotka.net/forums/t/7639.aspx


JohnyNFullEffect posted on Wednesday, September 16, 2009

Hey all,

I was working with the SQL2008 time data type today.  My table has two time(7) columns and I am looking to use our current CSLA implementation to get the values from the database.  We are on CSLA.NET 3.6.

I was looking at the SafeDataReader class and looking for something that would correctly get this data from the SafeDataReader.  Obviously, there is no GetTimeSpan() method.  What would work for this?  I read on an MSDN page that in SQL2008, the best data type in .NET is a TimeSpan.

Should I extend the class and write my own?  How do I figure out what is happening under the covers for something like SafeDataReader.GetSmartDate() to extend that to GetTimeSpan.

Thanks!

RockfordLhotka replied on Wednesday, September 16, 2009

SafeDataReader just implements IDataReader, automatically handling null values.

OK, so it also does string<->ordinal index translations and has GetSmartDate(), but otherwise it is just a wrapper over IDataReader.

If your specific database provider (such as SQL Server or Oracle) has custom types or other non-standard stuff in their particular datareader (such as SqlDataReader) you might consider creating your own wrapper around that particular datareader to tap into that non-standard functionality.

Turntwo replied on Tuesday, February 19, 2013

You could use 

(TimeSpan)datareader.GetValue("ReturnTime")

If your column allows nulls, then you would have to handle that yourself, because I'm pretty sure the GetValue method doesn't convert nulls for you (what would it convert it to?).  

JonnyBee replied on Wednesday, February 20, 2013

Or you could also write an extension method to add a GetTimeSpan method to IDataReader/SafeDataReader.

Copyright (c) Marimer LLC