Linq To Sql:调用具有不同返回值的存储过程
我有一个存储过程,根据输入返回多行整数或多行日期时间。
我查看了 这个问题 并已阅读并实施了 处理来自存储过程的多个结果形状(向下滚动页面以查看该部分。)
不幸的是,它不起作用。
这是我的代码:
[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;
}
}
使用 SQL Server Profiler,Linq 2 Sql 生成如下所示的代码:
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
生成的结果集如下所示:
cId
-----------
694
42
43
41
4732
-----------
0
(1 row(s) affected)
请注意,这里有两个结果集(应该只有一个。)并注意第二个结果集,返回给 LinqToSql 的结果集是 0!
“exec sp_executesql...”行生成第一个正确的结果集。
“select @p6”行产生第二个不正确的结果集。
首先,为什么@p6被定义为int?在函数中,我有两种可能的 ResultTypes :列表和列表 这似乎是一个问题。
其次,为什么它不返回存储过程的输出?而是返回 @p6 的值?
最后,最重要的是,我如何才能使其正常工作?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
忽略探查器 - 它会引导您走上错误的道路;-)
您如何阅读结果?
您应该将它们读入 IMultipleResults,然后调用 GetResult<>获取实际数据
IE。
或者
如果您选择了错误的类型并枚举了结果,您将抛出 SystemException(消息 =“指定的强制转换无效。”),因此如果您不知道类型,请假设最有可能的类型,并且如果抛出上述错误,请尝试其他类型。
像这样的东西:
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: