Getting a result feedback from a stored procedure in Entity Framework

后端 未结 4 1356
野性不改
野性不改 2021-01-21 09:02

In a SQL Server 2008 I have a simple stored procedure moving a bunch of records to another table:

CREATE PROCEDURE [dbo].MyProc(@ParamRecDateTime [datetime])
AS
         


        
4条回答
  •  南笙
    南笙 (楼主)
    2021-01-21 09:37

    For future reference: I had the same issue but needed multiple OUTPUT variables. The solution was a combination of both answers. Below is a complete sample.

    public void MyStoredProc(int inputValue, out decimal outputValue1, out decimal outputValue2)
    {
        var parameters = new[] { 
            new SqlParameter("@0", inputValue), 
            new SqlParameter("@1", SqlDbType.Decimal) { Direction = ParameterDirection.Output }, 
            new SqlParameter("@2", SqlDbType.Decimal) { Direction = ParameterDirection.Output } 
        };
    
        context.ExecuteStoreCommand("exec MyStoredProc @InParamName=@0, @OutParamName1=@1 output, @OutParamName2=@2 output", parameters);
    
        outputValue1 = (decimal)parameters[1].Value;
        outputValue2 = (decimal)parameters[2].Value;
    }
    

    Please note the Types used (decimal.) If another type is needed, remember to not only change it in the method argument list but also the SqlDbType.XXX.

提交回复
热议问题