Do not want to make multiple overloaded functions. Query Parameter Problem
there can be variable number of params being sent through a query string. in my form the query sends a few params that may not always be filled i.e. they will be sent as id=""
. this开发者_运维技巧 means that the function being used in my TableAdapter will bring the wrong result if one param is not coming in. i.e. it recieved 7 params whereas it was expecting 8 (or the 8th will be NULL).
The only workaround i can think is to make overloaded functions. but that means i will have to make 64 overloaded functions (for 8 params). Too much work, which makes me think that there maybe some other way i could get the job done without making 64 functions.
Is there any?
Working on ASP.NET with MSSQL
There is one work around we are using and It works perfectly for us.
You don't need to make any overload functions. You can pass all the parameter's values and If there is an empty value you can simply pass '%'
and you don't need to do anything with the query either. Here is an example of an SQL Query:
Select * from Student where ID = 5 AND RollNo = '%' AND CourseID = '%'
If you check the above query in SQL server it will give you correct result.
Note: I have not tested this with TableAdapter
, But I am sure it will work.
64 overloaded functions clearly isn't a workable solution. It's probably time to look at your WorkTable adapter code as it sounds like it isn't handling exceptional values very gracefully.
I would go with ashelvey's suggestion of one method with 8 parameters and make sure the handling code is robust enough to deal with null values or empty strings.
Introduce an object that is specifically for the parameters.
public void DoWork(params OperationArgs args[]) { /* code */ }
class OperationArgs {
public int? Id { get; set; }
public string Keywords { get; set; }
// etc
}
精彩评论