Answered by:
Date Type Mapping in C# for SQL 2008 Time and Date

Question
-
How should we map the new SQL 2008 time and date data types in C#? One suggestion a team member had was to create a user defined type for these values. Any other suggestions? Thank you for your help.Friday, November 7, 2008 3:09 PM
Answers
-
Microsoft recommends you map Date to DateTime (or Nullable<DateTime> if the column accepts null), and Time to Timespan (or again, Nullable<Timespan> if the column accepts null).
See this link for SQL server to .NET mapping.
David Morton - http://blog.davemorton.net/- Proposed as answer by Harry Zhu Tuesday, November 11, 2008 6:47 AM
- Marked as answer by Lettia Milito Tuesday, November 11, 2008 1:51 PM
Friday, November 7, 2008 3:22 PM -
Nullables make sense when communicating closely with the database; but it's not recommended that those nullables be visible everywhere: http://safari.informit.com/9780321580481/ch06
e.g. if you have a data layer, use nullables when getting data from the database; but the business logic should deal with all cases where the nullable is null and perform appropriate logic (like throw an exception or make the value Empty/default).
http://www.peterRitchie.com/blog- Edited by Peter Ritchie Friday, November 7, 2008 4:46 PM clarity
- Proposed as answer by Harry Zhu Tuesday, November 11, 2008 6:47 AM
- Marked as answer by Harry Zhu Wednesday, November 12, 2008 9:49 AM
Friday, November 7, 2008 4:45 PM
All replies
-
Microsoft recommends you map Date to DateTime (or Nullable<DateTime> if the column accepts null), and Time to Timespan (or again, Nullable<Timespan> if the column accepts null).
See this link for SQL server to .NET mapping.
David Morton - http://blog.davemorton.net/- Proposed as answer by Harry Zhu Tuesday, November 11, 2008 6:47 AM
- Marked as answer by Lettia Milito Tuesday, November 11, 2008 1:51 PM
Friday, November 7, 2008 3:22 PM -
Nullables make sense when communicating closely with the database; but it's not recommended that those nullables be visible everywhere: http://safari.informit.com/9780321580481/ch06
e.g. if you have a data layer, use nullables when getting data from the database; but the business logic should deal with all cases where the nullable is null and perform appropriate logic (like throw an exception or make the value Empty/default).
http://www.peterRitchie.com/blog- Edited by Peter Ritchie Friday, November 7, 2008 4:46 PM clarity
- Proposed as answer by Harry Zhu Tuesday, November 11, 2008 6:47 AM
- Marked as answer by Harry Zhu Wednesday, November 12, 2008 9:49 AM
Friday, November 7, 2008 4:45 PM -
Thanks for the help. When using SqlMetal to generate your dbml file these are the values that get created on the C# side from the Date and Time types on the SQL side. One thing to keep in mind is that you need to use the newest version of SqlMetal that ships with VS 2008 SDK (perhaps SP1) to use these new data types.Tuesday, November 11, 2008 1:56 PM