If I understood you correctly, than this is the article telling how to call a ms sql stored procedure with optional parameters. And this is how I used it to call such stored proc with LINQ to SQL:
1) Suppose you have a stored proc with optional parameters like:
CREATE PROCEDURE [dbo].[MyProc]
@arg1 bigint,
@arg2 datetime,
@arg3 decimal(18,2)=null,
@arg4 decimal(18,2)=null,
@arg5 int
BEGIN
...
END
2) You have some DataContext using LINQ to SQL
DataContext dc = new DataContext("Your connection string, for example");
3) Stored proc name
string procName = "dbo.MyProc";
4) Params dictionary (for example):
Dictionary<string, object> paramList = new Dictionary<string, object>()
{
{"arg1",72},
{"arg2",DateTime.Now.Date},
//arg3 is omitted and should get its default value in stored
{"arg4",250}, proc
{"arg5",777}
};
5) Then you may use the following method:
static int Foo(DataContext dc,
string procName,
Dictionary<string, object> paramList)
{
StringBuilder command = new StringBuilder("EXECUTE ");
command.Append(procName);
int i = 0;
foreach (string key in paramList.Keys)
{
command.AppendFormat(" @{0} = ", key);
command.Append("{");
command.Append(i++);
command.Append("},");
}
return dc.ExecuteCommand(command.ToString().TrimEnd(','),
paramList.Values.ToArray());
}
like this
//You should add exception handling somewhere, of course, as you need
Foo(dc, procName, paramList);
It will invoke your stored procedure. Compulsory params should always be present in the dictionary or it will fail. Optional parameters may be omitted, then they'll get the default values, defined by the stored procedure itself.
I used Dictionary<string,object>, so it may contain not only string values, but any type of parameters. Of course, they should reflect what the stored procedure expects.
P.S.: I tested on ms sql 2008, I'm not completely sure, how it'll work on ms sql 2005