gpt4 book ai didi

c# - 如何将 C# 中的日期字符串变量与 sql 查询中的日期时间字段进行比较

转载 作者:太空宇宙 更新时间:2023-11-03 11:17:10 25 4
gpt4 key购买 nike

我有以下代码,我正在尝试将日期字符串与日期时间进行比较,但我无法让它工作。这不会输出任何行。

DataTable tbl3 = dii.SelectGeneric("select * from widget_messages  where convert(datetime, m_date) ='" + String.Format("{0:d/M/yyyy}", DateUsed) + "'");

两个日期时间的格式完全相同。有什么想法吗?

最佳答案

您正在将 m_date 转换为 datetimeDateUsed 仍然是一个字符串。

你应该像这样将它们作为字符串进行比较

DataTable tbl3 = dii.SelectGeneric("select * from widget_messages  where m_date ='" + String.Format("{0:d/M/yyyy}", DateUsed) + "'");

或者作为日期时间,像这样:

DataTable tbl3 = dii.SelectGeneric("select * from widget_messages  where convert(datetime, m_date) = convert(datetime,'" + String.Format("{0:d/M/yyyy}", DateUsed) + "')");

编辑:在 SQL Server 2005 上试试这个:

DataTable tbl3 = dii.SelectGeneric("select * from widget_messages  where abs(datediff(day,convert(datetime, m_date), convert(datetime,'" + String.Format("{0:d/M/yyyy}", DateUsed) + "'))) = 0");

关于c# - 如何将 C# 中的日期字符串变量与 sql 查询中的日期时间字段进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12335213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com