BindingSource 按日期过滤 [英] BindingSource Filter by date
问题描述
我想根据日期从数据库中过滤值.
I want to filter values from database based on date.
数据库中的日期包含如下值:2008-12-28 18:00:00.我的班级有一个 DateTime 变量,具体取决于我要过滤的内容.理想情况下,它会像这样工作:myBindingSource.Filter = "DATE(myDateField) = myDateTime.Date"
+ 根据需要调整 myDateTime.Date 格式.
Date in a database contains values like this: 2008-12-28 18:00:00. And my class has a DateTime variable depending on which I want to filter. Ideally it would work like this:
myBindingSource.Filter = "DATE(myDateField) = myDateTime.Date"
+ adjusting myDateTime.Date format as needed.
但它抛出一个 EvaluateException:表达式包含未定义的函数调用 DATE()."
But it throws an EvaluateException: "The expression contains undefined function call DATE()."
虽然如果我直接执行SQL语句,我可以在过滤器中使用DATE()函数.
Although if I execute the SQL statement directly, I can use the DATE() function in filter.
附言我将 MYSQL DB 与 Connector/Net 5.2 一起使用
P.S. I use MYSQL DB with the Connector/Net 5.2
我该如何解决这个问题?
How can I solve this problem?
谢谢大家的建议.
推荐答案
不需要 getSqlDate 函数.您可以使用 String.Format() 来格式化日期:
The getSqlDate function is not needed. You can use String.Format() to format dates:
String.Format("{0:yyyy-MM-dd} 00:00:00", myDateTime)
或
myDateTime.Date.ToString("yyyy-MM-dd") + " 00:00:00"
您可以像这样过滤绑定源:
You could filter the binding source like this:
myBindingSource.Filter = String.Format("myDateField >= '{0:yyyy-MM-dd}' AND myDateField < '{1:yyyy-MM-dd}'", myDateTime, myDateTime.AddDays(1));
这篇关于BindingSource 按日期过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!