dotNet 如何处理编程日期没有时间但 sql 日期有时间的参数化日期

发布于 2024-09-10 07:37:37 字数 446 浏览 2 评论 0原文

可怕的标题对吧?我想看看堆栈溢出是否比我测试某些东西更快,同时我还受到其他工作的一千次干扰:)

我正在更新一个旧的 VB net 应用程序,并尝试重构一些逻辑。该应用程序在几个表中查找单个日期的数据,并将该视图写入文件。

在 SQL 中编写查询,我会得到相当于

SELECT * FROM table
WHERE CAST(FLOOR(CAST(table.date AS float))AS datetime) = '15-Jul-2010'

理想情况下我会使用

SELECT * FROM table WHERE date=@input

并将日期对象作为参数添加到 System.Data.SqlClient.SqlCommand 实例的结果,

这两者具有可比性吗?我会得到我期望的结果吗?

Dreadful title right? Thought I'd see if Stack overflow is quicker than me testing something while I get a thousand interruptions from other work :)

I'm updating an old VB net application and trying to refactor some of the logic as I go. The app looks for data from a single date across a few tables and writes that view to a file.

Writing the query in SQL I'd get the equivalent of

SELECT * FROM table
WHERE CAST(FLOOR(CAST(table.date AS float))AS datetime) = '15-Jul-2010'

Ideally I'd use

SELECT * FROM table WHERE date=@input

and add a date object as a parameter to a System.Data.SqlClient.SqlCommand instance

Are those two comparable? Will I get the results I expect?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

却一份温柔 2024-09-17 07:37:37

是的,两者是可比的,SqlClient 库会将 .net 类型转换为 sql 类型。您仍然需要截断 sql 查询中的时间部分,因此您可以使用类似的内容:

SELECT * FROM table WHERE FLOOR(CAST(table.date AS float)) = FLOOR(CAST(@input AS float))

您不必转换回日期时间,而只需比较浮点数。

Yes the two are comparable, the SqlClient library will convert .net types to sql types. You would still have to truncate the time part in your sql query, so you could use something like:

SELECT * FROM table WHERE FLOOR(CAST(table.date AS float)) = FLOOR(CAST(@input AS float))

You don't have to convert back to datetime, but can just compare the floats.

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