在 WHERE 子句中执行带有日期时间的查询
我使用sql server 2008 R2作为数据存储。
到目前为止,在测试机器上,我拥有该软件的英文版本,并用于进行格式化日期时间字段的查询,因为
fromDate.ToString("MM/dd/yyyy");
现在我已将数据库部署在另一台意大利语服务器上。我将代码中的格式更改为
fromDate.ToString("dd/MM/yyyy");
是否有办法以中性格式进行查询?
谢谢!
编辑:
我忘了提及我正在将 NetTiers 与 CodeSmith 一起使用。这是一个完整的示例
AppointmentQuery aq = new AppointmentQuery(true, true);
aq.AppendGreaterThan(AppointmentColumn.AppointmentDate, fromDate.ToString("MM/dd/yyyy"));
aq.AppendLessThan(AppointmentColumn.AppointmentDate, toDate.ToString("MM/dd/yyyy"));
AppointmentService aSvc = new AppointmentService();
TList<Appointment> appointmentsList = aSvc.Find(aq);
I use sql server 2008 R2 as a data store.
Until now on the test machine I had the english version of the software and used to make queries formatting the datetime field as
fromDate.ToString("MM/dd/yyyy");
now I have deployed the database on another server which is in the italian language. I shall change the format in my code to
fromDate.ToString("dd/MM/yyyy");
Is there a way to make the query in a neutral format?
thanks!
EDIT:
I forgot to mention that I am using NetTiers with CodeSmith. Here's a complete sample
AppointmentQuery aq = new AppointmentQuery(true, true);
aq.AppendGreaterThan(AppointmentColumn.AppointmentDate, fromDate.ToString("MM/dd/yyyy"));
aq.AppendLessThan(AppointmentColumn.AppointmentDate, toDate.ToString("MM/dd/yyyy"));
AppointmentService aSvc = new AppointmentService();
TList<Appointment> appointmentsList = aSvc.Find(aq);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
<罢工>
您应该共享用于执行查询的代码,但我猜您正在使用字符串连接动态构建 SQL 查询来构建查询和参数。您应该使用参数化查询,然后可以将数据作为日期对象传递,而无需转换字符串。
例如,如果您的查询可能是这样的
作为一个很好的副作用,这将降低SQL 注入的风险。
更新:在您进行编辑后,问题上下文确实发生了显着变化,我不得不承认我对 .netTiers 项目的了解为零。但出于好奇,您是否尝试过直接传递日期实例,如下所示?
You should share the code you are using to execute the query, but I guess you are building a SQL query dynamically using string concats to build the query and the arguments. You should rather use a parameterised query then you can pass the data as a date object and no need to converto a string.
For example if your query could be something like this
As a good side effect, this will reduce your risk of SQL injection.
Update: After your edit which does change the question context significantly, and I have to admit that I have Zero knowledge of the .netTiers project. But just out of curiosity have you tried just passing the date instances directly as in the following?
ISO 8601数据元素和交换格式 - 信息交换 - 日期和时间的表示 允许 YYYY-MM-DD 和 YYYYMMDD。 SQL Server 识别 ISO 规范。
我更喜欢 YYYYMMDD 格式,但我认为这是因为我一开始就知道这一点,对我来说,它似乎更通用,取消了可能被认为是特定于语言环境的字符。
ISO 8601 Data elements and interchange formats — Information interchange — Representation of dates and times allows both the YYYY-MM-DD and YYYYMMDD. SQL Server recognises the ISO specifications.
I prefer the YYYYMMDD format, but I think that's because I only knew about that to start with, and to me it seems more universal, having done away with characters that might be considered locale specific.
就我个人而言,我总是使用
yyyy-MM-dd
。这也使得它可以作为字符串排序。然而,日期就是日期。无需将日期更改为字符串。在 .NET 中,用户
DateTime
。Personally, I always use
yyyy-MM-dd
. This also makes it sortable as a string.However, a date is a date is a date. There's no need to change the date to a string. In .NET, user
DateTime
.