Handling Optional .NET DataSource Parameters
What is the standard way to implement optional query parameters in a .NET WinForms application?
In other words, only query on a field if the value of a corresponding control is not null.
Note: Prefer VB.开发者_StackOverflow社区NET answers (C# okay too)
EDIT: I use a FillBy method which calls a query in my Access database. In the TableAdapter query editor, I just used WHERE (field1 = ?) AND (field2 = ?) ...
I just can't find the "hook" to bind form controls to the table adapter query parameters and so that if a form uses the default value to not query on it.
My Access syntax is rusty, so you might have to adjust it, but you can try something like this:
WHERE (@Field1 IS NOT NULL AND Field1 = @Field1) AND (@Field2 IS NOT NULL AND Field2 = @Field2)
You can apply LINQ Where
clause many times.
If you're working with a DataSet
, you can call AsEnumerable
(available since .NET 3.5) to do LINQ queries on it.
精彩评论