C# + MSSQL + DateTime

WJPS used Ask the Experts™

I am trying to save a date in MSSQL from a C#.net application. However i always seem to have similar problems. If i use a date time varible then i get the time as well by default, and if i just use a string i have alsorts of complication when it comes to ordering unless i store it backwards. Can you suggest the most appropriate way to storing and retriving a date from a database.

Any example would be great.


Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Billing Engineer
Most Valuable Expert 2014
Top Expert 2009
store it as datetime, and "cut" the time to 0:00:00.
if you really want to store as varchar, then use the format YYYY-MM-DD HH24:MI:SS

the best way to run sql statement is with SqlParameter objects in the SqlCommand, which helps to define the data types properly.
to "cut" the time, in C#, you have methods in the Datetime datatype to do that easily.
AneeshDatabase Consultant
Top Expert 2009

you can store it as either DateTime or SmallDateTime

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial