SqlDateTime and DateTime

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

On my Linq To Sql tables the properties are of type DateTime.

The problem is that sometimes instead of filling a DateTime field with
null or set them to not null and insert minimum DateTime value ... But
the C# one is different from the SQL one.

So I need to do very often the following:

DateTime user.LastLocked = SqlDateTime.MinValue;

Which does not work ... So I created a simple extenstion to convert
SqlDateTime to DateTime:

public static DateTime ToDateTime(this SqlDateTime value) {
return DateTime.Parse(value.ToString());
} // Random

Is this ok?

Maybe I should include something about MinValue?
And should I use ConvertTo instead of Parse?

Just trying to have this working in all situations.

Thanks,
Miguel
 
shapper said:
Hello,

On my Linq To Sql tables the properties are of type DateTime.

The problem is that sometimes instead of filling a DateTime field with
null or set them to not null and insert minimum DateTime value ... But
the C# one is different from the SQL one.

So I need to do very often the following:

DateTime user.LastLocked = SqlDateTime.MinValue;

Which does not work ... So I created a simple extenstion to convert
SqlDateTime to DateTime:

public static DateTime ToDateTime(this SqlDateTime value) {
return DateTime.Parse(value.ToString());
} // Random

Is this ok?

Maybe I should include something about MinValue?
And should I use ConvertTo instead of Parse?


The struct SqlDateTime contains an explicit conversion operator for
converting to DateTime, so this should work:

DateTime user.LastLocked = (DateTime)SqlDateTime.MinValue;

I don't know how this conversion operator handles the MinValue, since
both types use different values (1/1/1 for DateTime, and 1/1/1753 for
SqlDateTime), so you may want to check for this special case if the
distinction is relevant to your application.
 
     The struct SqlDateTime contains an explicit conversion operator for
converting to DateTime, so this should work:

     DateTime user.LastLocked = (DateTime)SqlDateTime.MinValue;

     I don't know how this conversion operator handles the MinValue

It handles ok ... I tried that before but I got an error ...
I think the error was related to something else and I didn't realize
it ...

Thanks,
Miguel
 
Back
Top