I'm trying to insert 'DateTime.Now' into sql column type of datetime but when insert is done in sql
table date is OK! (exp: 2015-02-11) but time is stored as 00:00:00!!! I tried to many alternates.
this is my code:
sqlComm.Parameters.Add("@join_date", SqlDbType.Date).Value = DateTime.Now.ToString("yyyy-MM-dd h:m:s");
What am I missing???
You're specifying a SqlDbType
of Date
- that doesn't have a time. So when the driver is converting the parameter value into the desired type, it's removing the time part.
You're also converting DateTime.Now
into a string for no obvious reason... and doing so in a way which uses an odd and lossy time format (h:m:s
, with no AM/PM designator). If you really did want to convert to a string, it would be better to use HH:mm:ss
for the time specifier.
This line:
sqlComm.Parameters.Add("@join_date", SqlDbType.Date).Value = DateTime.Now.ToString("yyyy-MM-dd h:m:s");
should be:
sqlComm.Parameters.Add("@join_date", SqlDbType.DateTime).Value = DateTime.Now;
See more on this question at Stackoverflow