Question

I am using version 3.8.5.0 of ServiceStack.ormLite.dll.

We are using postgreSql server. Our postgreSQL server has it locale set to en-GB (in postgres.conf we have set dateStyle parameter to "ISO, DMY").

In our web application, when we pass a raw SQL statement as:

  var insertSql = string.Format("insert into ReportTable values (
                                    '{0}', '{1}',)",
                                          message.Id, DateTime.Now);

and use ExecuteSql(insertSql), I get date-range out of date in postgres as the date is in MDY and the database expects DMY.

However if I do:

    var insertSql = string.Format("insert into ReportTable values (
                                    '{0}', 'now',)",
                                          message.Id);

the insert works.

I know I could set

 <globalization Culture="en-GB"/>

However, I have some threads that do some background processing and I will have to change their culture as well to get this working.

Any ideas if there is any other way I can solve this issue on the ormLite library?

Was it helpful?

Solution

You must format DateTime.Now the way postgreSQL (or any RDBMS for that matter) expects it.

var insertSql = string.Format("insert into ReportTable values (
                                '{0}', '{1:your format here}',)",
                                      message.Id, DateTime.Now);

For example, if your RDBMS expects dates in the day-month-year format, then you need to pass this:

var insertSql = string.Format("insert into ReportTable values (
                                '{0}', '{1:dd-MM-yyyy}',)",
                                      message.Id, DateTime.Now);

Of course you could also use parameter objects as Jon Skeet suggested, although there could be a reason why you specifically want to use a string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top