简体   繁体   English

ORMLite的ExecuteSQL调用中dateTime.Now的格式是什么?

[英]What is the format of dateTime.Now in ExecuteSQL call of ORMLite?

I am using version 3.8.5.0 of ServiceStack.ormLite.dll. 我正在使用ServiceStack.ormLite.dll的3.8.5.0版本。

We are using postgreSql server. 我们正在使用postgreSql服务器。 Our postgreSQL server has it locale set to en-GB (in postgres.conf we have set dateStyle parameter to "ISO, DMY"). 我们的postgreSQL服务器将其语言环境设置为en-GB(在postgres.conf中,我们将dateStyle参数设置为“ ISO,DMY”)。

In our web application, when we pass a raw SQL statement as: 在我们的Web应用程序中,当我们将原始SQL语句传递为:

  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. 并使用ExecuteSql(insertSql),由于日期在MDY中并且数据库期望DMY,所以我在postgres中获取了日期范围过期的数据。

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? 有没有其他想法可以解决ormLite库上的此问题?

You must format DateTime.Now the way postgreSQL (or any RDBMS for that matter) expects it. 您必须格式化DateTime.Now现在以postgreSQL(或任何RDBMS)期望的方式进行格式化。

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: 例如,如果您的RDBMS期望以年-月-年格式显示日期,那么您需要传递以下信息:

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 . 当然,您也可以按照Jon Skeet的建议使用参数对象,尽管可能有一个您特别想使用string

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM