按日期BindingSource的过滤器 [英] BindingSource Filter by date

查看:406
本文介绍了按日期BindingSource的过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想筛选来自基于最新数据库值

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语句直接,我可以在过滤器中使用的日期()函数。

Although if I execute the SQL statement directly, I can use the DATE() function in filter.

P.S。我使用MySQL数据库的连接器/净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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆