Getting a result feedback from a stored procedure in Entity Framework

后端 未结 4 1352
野性不改
野性不改 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:53

    One way to do it is to call ExecuteStoreCommand, and pass in a SqlParameter with a direction of Output:

    var dtparm = new SqlParameter("@dtparm", DateTime.Now);
    var retval = new SqlParameter("@retval", SqlDbType.Int);
    
    retval.Direction = ParameterDirection.Output;
    
    context.ExecuteStoreCommand("exec @retval = MyProc @dtparm", retval, dtparm);
    
    int return_value = (int)retval.Value;
    

    Originally I tried using a direction of ReturnValue:

    retval.Direction = ParameterDirection.ReturnValue;
    
    context.ExecuteStoreCommand("MyProc @dtparm", retval, dtparm);
    

    but retval.Value would always be 0. I realized that retval was the result of executing the MyProc @dtparm statement, so I changed it to capture the return value of MyProc and return that as an output parameter.

提交回复
热议问题