在将数据插入SQL Server时使用ToAODate()会导致表中的日期错误 [英] using ToAODate() while inserting data to SQL Server results in wrong date in table

查看:203
本文介绍了在将数据插入SQL Server时使用ToAODate()会导致表中的日期错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我必须将当前日期时间值插入或更新到sqlserver表.
因为我不知道sqlserver的datetime culure设置,所以在某些系统上插入诸如mm-dd-yyyy HH:mm之类的内容失败,或者当日期和月份低于13时导致错误的值.

尝试创建解决此问题的方法,我插入了c#DateTime.Now.ToAODate().这似乎有些奇怪,但是仔细查看数据库中的结果,每个日期都是未来2天的结构化日期(例如,插入日期21-10-2010 15:40时,我得到23-10-2010 15:40现在,我只需在插入数据之前减去2天.

这是一个已知问题还是我做错了什么?

问候,

Hi,

I have to insert or update a current datetime value to a sqlserver table.
because I don''t know the datetime culure settings for the sqlserver, inserting something like mm-dd-yyyy HH:mm fails on some systems or results in incorrect values when day and month is below 13.

Trying to create a workaround for this problem I insert the c# DateTime.Now.ToAODate(). this seems to wirk, but looking closer at the result in the database, every date is structural 2 days in the future (eg. when inserting the date 21-10-2010 15:40 I get 23-10-2010 15:40 in the database. For now I simply substract 2 days before inserting the data.

Is this a know problem or am''i doing something wrong?

greetings,

推荐答案

您不能只使用GETDATE()吗?

Can you not just use GETDATE()?

INSERT INTO 
    MyTable (IntField, StringField, DateField)
VALUES
    (1, ''test'', GETDATE())




我发现了一个与此问题有关的帖子.看来这是一个已知问题,您需要有一点技巧来修复它.链接如下:

此处 [
当然,如果您使用数据集或其他脱机机制,这并不总是适合使用的方法.

祝你好运!
戴夫
Hi,

I found a post relating to this issue. It seems this is a known issue and you need to have a bit of a hack to fix it. Link is below:

Here[^]

If you have rights to the database and to modify tables, you can set the column properties for the date column to have a "Default Value or Binding" insert the TSQL method GETDATE() as the default value. That way you don''t have to worry about having DateTime objects floating around in your code. The dates will get generated when the data gets written to the database.

Ofcourse, this is not always an appropriate method to use if you use datasets or other offline mechanisms.

good luck!
Dave


这篇关于在将数据插入SQL Server时使用ToAODate()会导致表中的日期错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆