简体   繁体   中英

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. It is returning multiple resultsets as well as a couple output parameters. 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. The 'UserHasAccessToOption' parameter always returns false, even though it is only set to true in the Stored Procedure.

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.

Here is my code to access the stored procedure and return the 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.

I recommend you see the "Handling Multiple Result Shapes from SPROCs" section of this valuable article:

LINQ to SQL (Part 6 - Retrieving Data Using Stored Procedures)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM