JD.
JD.

Reputation: 15551

How to set DateTime.Now when inserting in postgresql table?

We ship images and the default culture setting for our image is MDY.

The customer server where the image is installed has a locale of DMY has been using our image for a few days and then noticed datestyle errors with dates (i.e. when inserting a DateTime.Now() into a table).

We have tried changing the postgreSQL locale to DMY (in postgresql.conf) but with no luck.

I was thinking about converting all DateTime.Now() calls in my C# app to DMY format.

I also noticed that I could set in my web.config which may help.

As I cannot seem to change the postgreSQL settings what are my options?

Another option I was thinking was to uninstall postgreSQL and then install it again with the correct locale set (as long as I backup up my database). Hopefully this will work.

Upvotes: 0

Views: 6129

Answers (1)

Damith
Damith

Reputation: 63105

if you need to insert current time send the value as now

   INSERT INTO TestTable (code, title,  date)
        VALUES ('aaa', 'bbbb', now);

you can use parameters for other columns, let server insert the current date time value from database side

if you going to set datetime parameter, don't set value as string. do as below

string sql  ="INSERT INTO TestTable (dateTimeColumnName) VALUES (:datetimeParamName)";
// create connection cmd etc...
cmd.Parameters.AddWithValue(":datetimeParamName", DateTime.Now);

OR

NpgsqlParameter param = new NpgsqlParameter(":datetimeParamName",NpgsqlTypes.NpgsqlDbType.Date);
param.Value = DateTime.Now;
cmd.Parameters.Add(param); 

Upvotes: 5

Related Questions