在 WHERE 子句中执行带有日期时间的查询

发布于 2024-09-26 04:36:31 字数 738 浏览 3 评论 0原文

我使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嘿哥们儿 2024-10-03 04:36:31

<罢工>
您应该共享用于执行查询的代码,但我猜您正在使用字符串连接动态构建 SQL 查询来构建查询和参数。您应该使用参数化查询,然后可以将数据作为日期对象传递,而无需转换字符串。

例如,如果您的查询可能是这样的

DateTime fromDate = DateTime.Now;

SqlCommand cmd = new SqlCommand(
  "select * from Orders where fromDT = @fromDate", con);

cmd.Parameters.AddWithValue("@fromDate", fromDate);

...

作为一个很好的副作用,这将降低SQL 注入的风险。

更新:在您进行编辑后,问题上下文确实发生了显着变化,我不得不承认我对 .netTiers 项目的了解为零。但出于好奇,您是否尝试过直接传递日期实例,如下所示?

AppointmentQuery aq = new AppointmentQuery(true, true); 
aq.AppendGreaterThan(AppointmentColumn.AppointmentDate, fromDate); 
aq.AppendLessThan(AppointmentColumn.AppointmentDate, toDate); 
AppointmentService aSvc = new AppointmentService(); 
TList<Appointment> appointmentsList = aSvc.Find(aq); 


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

DateTime fromDate = DateTime.Now;

SqlCommand cmd = new SqlCommand(
  "select * from Orders where fromDT = @fromDate", con);

cmd.Parameters.AddWithValue("@fromDate", fromDate);

...

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?

AppointmentQuery aq = new AppointmentQuery(true, true); 
aq.AppendGreaterThan(AppointmentColumn.AppointmentDate, fromDate); 
aq.AppendLessThan(AppointmentColumn.AppointmentDate, toDate); 
AppointmentService aSvc = new AppointmentService(); 
TList<Appointment> appointmentsList = aSvc.Find(aq); 
南汐寒笙箫 2024-10-03 04:36:31

ISO 8601数据元素和交换格式 - 信息交换 - 日期和时间的表示 允许 YYYY-MM-DD 和 YYYYMMDD。 SQL Server 识别 ISO 规范。

虽然标准允许
YYYY-MM-DD 和 YYYYMMDD 格式
完整的日历日期
表示,如果 [DD] 日是
省略则仅使用 YYYY-MM 格式
是允许的。通过禁止日期
YYYYMM 形式,标准避免
与被截断的混淆
表示 YYMMDD(仍然经常
使用)。

我更喜欢 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.

Although the standard allows both the
YYYY-MM-DD and YYYYMMDD formats for
complete calendar date
representations, if the day [DD] is
omitted then only the YYYY-MM format
is allowed. By disallowing dates of
the form YYYYMM, the standard avoids
confusion with the truncated
representation YYMMDD (still often
used).

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.

故事和酒 2024-10-03 04:36:31

就我个人而言,我总是使用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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文