Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to filter values from database based on 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.

But it throws an EvaluateException: "The expression contains undefined function call DATE()."

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

P.S. I use MYSQL DB with the Connector/Net 5.2

How can I solve this problem?

Thank You all for suggestions.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
542 views
Welcome To Ask or Share your Answers For Others

1 Answer

The getSqlDate function is not needed. You can use String.Format() to format dates:

String.Format("{0:yyyy-MM-dd} 00:00:00", myDateTime)

OR

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));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...