是否有可能返回一个返回值和使用LINQ存储过程的结果集 [英] Is it possible to return a return value and a result set from a stored procedure using LINQ

查看:126
本文介绍了是否有可能返回一个返回值和使用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).

MY存储的特效有返回值来显示错误,等等,他们用select语句来获取信息结束。与常规命令,就创造条件,保存返回值的输出参数。我不能改变在数据库中存储的特效,所以我无法通过其他则不同输出参数的返回值。

MY stored procs have return values to show errors, etc and they end with a select statement to get the information. 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.

想法??

推荐答案

当你从服务器资源管理器拖动它创建的LINQ到SQL数据上下文设计的存储过程,设计师将分析存储过程来确定如果它返回一个结果集。除非它拥有厚重的动态SQL,它通常是即期。

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. Unless it features heavy dynamic SQL, it's usually spot on.

如果确实如此,该方法产生的收益 ISingleResult< T> ,其中 T 是从您的存储过程的分析生成适合结果集的您在存储过程属性页中指定的形状,或者一个类。因为 ISingleResult 的IEnumerable 派生,大多数人做的结果,一个简单的foreach得到他们的结果集。但大多数人忘记的是, ISingleResult 的从 IFunctionResult 派生,它有一个属性所谓 返回值 。的这是从存储过程返回值

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. Because ISingleResult derives from IEnumerable, most people do a simple foreach on the result to get their result set. But what most people forget is that ISingleResult also derives from IFunctionResult, which has a property called 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
    }
}

这篇关于是否有可能返回一个返回值和使用LINQ存储过程的结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆