Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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,
Posted

Can you not just use GETDATE()?

INSERT INTO 
    MyTable (IntField, StringField, DateField)
VALUES
    (1, 'test', GETDATE())
 
Share this answer
 
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900