Reputation: 5915
I am using Linq to SQL with stored procedures.
I have to pass parameters to stored procedures even if they are optional (where I have set the default value for parameters in stored procedures). Is there any way that I may skip passing default parameters.
Thanks
Upvotes: 4
Views: 5717
Reputation: 11611
As mentioned link in marked answer is dead, I put the idea here for future readers (of course I didn't read the mentioned article, but below way will work, I tested before posting here)
You need to overload auto-generated method of your SP
(in .designer.cs class) and cut your optional parameter(s) (as you know, By default, when you drop a stored procedure onto DBMLclass
file, it only auto-generates the default function)
For ex. with this SP
:
CREATE PROCEDURE sp_Test
@optionalParam INT = 0
AS
-- your logic gose here
In .designer.cs
you will got something like below by default
[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.sp_Test")]
public ISingleResult<sp_TestResult> sp_Test([global::System.Data.Linq.Mapping.ParameterAttribute(Name="optionalParam", DbType="Int")] System.Nullable<int> optionalParam)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), optionalParam);
return ((ISingleResult<sp_TestResult>)(result.ReturnValue));
}
So you need to overload
this, here is the code:
[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.sp_Test")]
public ISingleResult<sp_sp_TestResult> sp_Test()
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())));
return ((ISingleResult<sp_sp_TestResult>)(result.ReturnValue));
}
Remember
DBML designer file
will be re-generated every time you add/remove any SP or table to it, so make sure to keep your methods before you do save on it, or alternatively move all overloaded methods to a new partial class.Upvotes: 4
Reputation: 10623
You can do this: http://challadotnetfaq.blogspot.com/2009/05/stored-procedure-optional-parameters.html
you can if you map the sp to a method with optional parameters
Upvotes: 2