[英]Is it possible to return a return value and a result set from a stored procedure using LINQ
Looking for suggestions on cleanest way to get the return value and the result set (without passing a referenced parameter to the stored proc). 寻找有关获取返回值和结果集的最干净方法的建议(不将引用的参数传递给存储的proc)。
MY stored procs have return values to show errors, etc and they end with a select statement to get the information. MY存储的proc具有返回值以显示错误等,并且它们以select语句结尾以获取信息。 With regular commands I would create an output parameter which would hold the return value.
使用常规命令,我将创建一个包含返回值的输出参数。 I can't change the stored procs in the db so I can't pass a different output parameter other then the return value.
我无法更改数据库中存储的proc,因此除了返回值之外,我无法传递其他输出参数。
thoughts?? 想法?
When you create a stored procedure in the LINQ to SQL Data Context designer by dragging it from the Server Explorer, the designer will analyze the sproc to determine if it returns a result set. 在LINQ to SQL Data Context设计器中通过从Server Explorer拖动存储过程来创建存储过程时,设计器将分析该存储过程,以确定它是否返回结果集。 Unless it features heavy dynamic SQL, it's usually spot on.
除非它具有沉重的动态SQL,否则通常都是这样。
If it does, the method generated returns ISingleResult<T>
where T
is either a class generated from an analysis of your sproc to fit the shape of the result set, or one you specify in the sproc properties page. 如果是这样,则生成的方法将返回
ISingleResult<T>
,其中T
是通过对存储过程进行分析以适合结果集形状而生成的类,或者是在存储属性页中指定的类。 Because ISingleResult
derives from IEnumerable
, most people do a simple foreach on the result to get their result set. 因为
ISingleResult
派生自IEnumerable
,所以大多数人都对结果进行简单的foreach获取其结果集。 But what most people forget is that ISingleResult
also derives from IFunctionResult
, which has a property called ReturnValue
. 但是大多数人都忘记了
ISingleResult
也是从IFunctionResult
派生的, IFunctionResult
具有名为ReturnValue
的属性。 This is the return value from the sproc. 这是存储过程的返回值。
So all you have to do is something along the lines of: 因此,您所要做的只是以下几方面的事情:
using(var dc = new MyDataContext())
{
var results = dc.MyStoredProcedure();
var returnValue = (int)results.ReturnValue; // note that ReturnValue is of type object and must be cast.
foreach(var row in results)
{
// process row
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.