Safe Save DateTime to SQL Database.

When I tried to save DateTime data to SQL database, I've got 
SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
I've had to create a function
object SafeSqlDateTime(DateTime dtValue)
{
object theValue = dtValue;
if (dtValue <(DateTime) SqlDateTime.MinValue) // January 1, 1753.
{
   theValue =
null;
}
return theValue;
For opposite SqlDateTime to DateTime Conversion see
http://msdn.microsoft.com/en-us/library/aa326475(VS.71).aspx
 

posted @ Sunday, May 03, 2009 9:13 PM

Print

Comments on this entry:

# re: Safe Save DateTime to SQl Database.

Left by Max Schmeling at 5/5/2009 3:31 AM
Gravatar
If you're using DateTime.MinValue for the same reason eric is in that blog post you referenced, why don't you just use a Nullable<DateTime> instead?

# re: Safe Save DateTime to SQl Database.

Left by Michael Freidgeim at 5/5/2009 8:08 PM
Gravatar
Max,
To use Nullable<DateTime> (or DateTime? ) is a good idea in general, but I have legacy code, that uses DateTime with valid data most of the time, and only in special cases it is optional and initialized as 1/1/0001

Your comment:



 (will not be displayed)


 
 
 
 
 

Live Comment Preview:

 
«November»
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345