Question

I like a lot of the built in features the built-in DataSet designer provides in VS2010 and do not want to have to change to something entirely different if at all possible. The problem is, to have optional parameters, I need to create completely independent functions for each combination of parameters. So for 6 parameters, I would need 63 different functions. That's obviously completely unmanageable.

Is there a way to have one function which ONLY adds a parameter to the generated WHERE clause of my SQL if it has a value, and otherwise, it ignores it?

Was it helpful?

Solution

You could add these optional paremeters into the WHERE-Clause with ISNULL:

WHERE (YourTable.Column = ISNULL(@Column, YourTable.Column))

On this way the SQL works with or without the (optional) parameter. If the value is null it won't affect the result. You only have to add all parameters into the DataAdapter's parameter-collection(the optional with AllowDbNull=true). But i'm fairly sure that they will be automatically added correctly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top