How to pass datetime from c# to sql correctly?

后端 未结 2 729
情歌与酒
情歌与酒 2020-12-05 07:07

I have a table and the date-times in it are in the format:

2011-07-01 15:17:33.357

I am taking a c# DateTime when I do a .ToString() on the

2条回答
  •  爱一瞬间的悲伤
    2020-12-05 07:42

    You've already done it correctly by using a DateTime parameter with the value from the DateTime, so it should already work. Forget about ToString() - since that isn't used here.

    If there is a difference, it is most likely to do with different precision between the two environments; maybe choose a rounding (seconds, maybe?) and use that. Also keep in mind UTC/local/unknown (the DB has no concept of the "kind" of date; .NET does).

    I have a table and the date-times in it are in the format: 2011-07-01 15:17:33.357

    Note that datetimes in the database aren't in any such format; that is just your query-client showing you white lies. It is stored as a number (and even that is an implementation detail), because humans have this odd tendency not to realise that the date you've shown is the same as 40723.6371916281. Stupid humans. By treating it simply as a "datetime" throughout, you shouldn't get any problems.

提交回复
热议问题