简体   繁体   English

如何使用 IMultipleResults 并从 SQL 存储过程返回 Output 参数?

[英]How can I use IMultipleResults AND return Output Parameters from a SQL Stored procedure?

I have a SQL Stored Procedure that I am accessing using Linq-To-SQL.我有一个 SQL 存储过程,我正在使用 Linq-To-SQL 访问它。 It is returning multiple resultsets as well as a couple output parameters.它返回多个结果集以及一对 output 参数。 It works when I run the procedure as a Query within SQL, but when I try to access it from my C# code, it does not want to work.当我在 SQL 中将过程作为查询运行时,它可以工作,但是当我尝试从我的 C# 代码访问它时,它不想工作。 The 'UserHasAccessToOption' parameter always returns false, even though it is only set to true in the Stored Procedure. 'UserHasAccessToOption' 参数始终返回 false,即使它在存储过程中仅设置为 true。

Again, it returns and allows me to browse the other ResultSets that are returned by the Stored Procedure, but it does not return ANY SQL output parameters.同样,它返回并允许我浏览存储过程返回的其他结果集,但它不返回任何 SQL output 参数。

Here is my code to access the stored procedure and return the IMultipleResults object:这是我访问存储过程并返回 IMultipleResults object 的代码:

    [FunctionAttribute(Name = "dbo.GetOption")]
    [ResultType(typeof(Option))]
    [ResultType(typeof(LoanPurpose))]
    [ResultType(typeof(LoanType))]
    [ResultType(typeof(User))]
    [ResultType(typeof(Client))]
    [ResultType(typeof(OrganizationFinancialItem))]
    public IMultipleResults GetOption(
        int? OptionID, 
        int UserID, 
        bool GetClients, 
        bool GetOtherOrganizationUsers, 
        bool GetLoanPurposes, 
        bool GetLoanTypes, 
        bool GetOrganizationFinancialItems,
        ref bool? UserHasAccessToOption)
    {
        using (profiler.Step("Call \"GetOption\" SProc"))
        {
            IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod()))
                , OptionID
                , UserID
                , GetClients
                , GetOtherOrganizationUsers
                , GetLoanPurposes
                , GetLoanTypes
                , GetOrganizationFinancialItems
                , UserHasAccessToOption);
            return (IMultipleResults)(result.ReturnValue);
        }
    }

The values of the output parameters are sent last in the data stream from the database, so you have to read to the end of the last result set before you can get the output parameters. output参数的值在数据库中的数据stream中最后发送,所以必须读取到最后一个结果集的末尾才能获取Z78E6221F6393D1356681DB398F14CED参数。

I recommend you see the "Handling Multiple Result Shapes from SPROCs" section of this valuable article:我建议您查看这篇有价值的文章的“处理来自 SPROC 的多个结果形状”部分:

LINQ to SQL (Part 6 - Retrieving Data Using Stored Procedures) LINQ 到 SQL(第 6 部分 - 使用存储过程检索数据)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从.NET中的存储过程返回oracle输出参数 - How to return oracle output parameters from a stored procedure in .NET 如何从 SQL 存储过程返回输出 varchar 值 - How to return output varchar value from SQL stored procedure 如何在C#中从Web服务的存储过程中获取多个输出参数? - How can I get multiple output parameters from a stored procedure in a web service in C#? 由于无参数构造函数,使用IMultipleResults从存储过程填充KeyValuePair失败 - Populating KeyValuePair from stored procedure with IMultipleResults fails due to parameterless constructor 如何从SQL Server中的存储过程检索参数列表 - How can I retrieve a list of parameters from a stored procedure in SQL Server 从SQL存储过程c#加载输出参数 - load Output parameters from SQL stored procedure c# 如何从C#函数的存储过程中返回多个输出参数 - How to return multiple output parameters from stored procedure for C# function 具有输出/输入参数返回计数的存储过程 - Stored procedure with output / input parameters return count IMul​​tipleResults:当存储的proc不映射到类型时,如何处理多个结果集? - IMultipleResults: how do I deal with multiple result sets from a stored proc when they don't map to types? 如何从存储过程中获取返回值? - How can I get the return value from my stored procedure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM