开发者

C#+linq - Inserting DateTime value to the Db in the right format

开发者 https://www.devze.com 2022-12-22 03:39 出处:网络
I have a Db server with DateTime fields in the format of \"yyyy-MM-dd mm:hh:ss\" I\'m trying to use linq2sql to insert a DateTime member to a DateTime field in one of my tables.

I have a Db server with DateTime fields in the format of "yyyy-MM-dd mm:hh:ss"

I'm trying to use linq2sql to insert a DateTime member to a DateTime field in one of my tables.

When I do it in SQL I convert the DateTime as following:

"Insert into .... Convert(datetime, getdate()开发者_Go百科, 120) ..."

But when I try to submit the object from Linq the date time inserts in the wrong format.

Is there a way to define the format that will be inserted to the Db in Linq? Or is it a DateTime object Issue?


You shouldn't be dealing with a string format when you pass dates and times to the database, any more than you would if you were passing a number. The database should be handling all this for you without any conversions. This is true whether you're using LINQ or within the SQL - almost any time you have to manually do string conversions between types at the database level, you should look for a better solution.

If you read the value back out of the database (as a DateTime again) does it have the right value? If not, in what way is it wrong?

0

精彩评论

暂无评论...
验证码 换一张
取 消