I have a stored procedure that, depending on the input, returns multiple rows of ints or multiple rows of DateTimes.
I have looked at the answers to this question and have read and implemented Scott Guthries solution from Handling Multiple Result Shapes from SPROCs (scroll down the page to see that section.)
Unfortunately, it just is not working.
Here is my code:
[Function(Name = "FunkyStoredProcedure")]
[ResultType(typeof(List<int>))]
[ResultType(typeof(List<DateTime>))]
public IMultipleResults FunkyStoredProcedure(int appId, int sId, int cId)
{
IExecuteResult result = this.ExecuteMethodCall(this, (MethodInfo)(MethodInfo.GetCurrentMethod()), appId, sId, cId);
return (IMultipleResults)result.ReturnValue;
}
}
Using SQL Server Profiler, Linq 2 Sql is producing code that looks like this:
declare @p6 int
set @p6=0
exec sp_executesql N'EXEC @RETURN_VALUE = [FunkyStoredProcedure] @appId = @p0, @sId = @p1, @cId = @p2',N'@p0 int,@p1 int,@p2 int,@RETURN_VALUE int output',@p0=2,@p1=4,@p2=2,@RETURN_VALUE=@p6 output
select @p6
Which produces a result set that looks like this:
cId
-----------
694
42
43
41
4732
-----------
0
(1 row(s) affected)
Note that there are two result sets here (there should be only one.) And note that the second result set, the one that is being returned to LinqToSql is 0!
The line “exec sp_executesql…” produces the first, correct, result set.
The line “select @p6” produces the second, incorrect, result set.
First of all, why is @p6 being defined as an int? In the function I have two possible ResultTypes : List and List This seems like an issue right here.
Second, why is it not returning the output of the stored procedure? And instead returning the value of @p6?
Finally, most important, how do I get this to work correctly?
Ignore the profiler – it is leading you down the wrong path 😉
How are you reading the results?
You should be reading them into an IMultipleResults, and then calling GetResult<> to get the actual data
ie.
or
If you choose the wrong type and enumerate through the results, you will throw a SystemException (message = “Specified cast is not valid.”), so if you do not know the type, assume the one that is most likely, and if that throws the above, try the other type instead.
Something like: