How to convert SqlDataTypes to System Types

  • Thread starter Thread starter Earl Teigrob
  • Start date Start date
E

Earl Teigrob

I am having a problem converting Sql data types to .Net value types
for example, the following code

SqlDateTime sd = new SqlDateTime(2004,2,2);

DateTime d;

d=Convert.ToDateTime(sd);



produces this error message


Specified cast is not valid.


The microsoft help to convert SqlDateTime to DateTime for C# shows the code
below but I am not sure how to use this to do a conversion

[C#]
public static explicit operator DateTime(
SqlDateTime x
);

Does anyone have an example for doing this?

Thank you

Earl
 
Earl,

In order to take advantage of this, you can just perform a cast, like
so:

DateTime d = (DateTime) sd;

Hope this helps.
 
Back
Top