使用LINQ访问存储过程值。实体框架5,DB优先

时间:2021-08-24 02:07:14

My stored procedure is working correctly. However, I am not able to retrieve it.

我的存储过程运行正常。但是,我无法取回它。

My current function to retrieve the value from the stored procedure is:

我从存储过程中检索值的当前函数是:

    public static int GetCsStatus()
    {
        using (Entities db = new Entities())
        {
            System.Data.Objects.ObjectParameter s = new System.Data.Objects.ObjectParameter("Status", typeof(int));
            int r = db.proc_CsStatus(120, s);//.ToString());
            return r;
        }
    }

I don't mind if this is changed or not used at all. I am currently getting a "r" value of -1 when I am expecting a 0 or 1.

我不介意这是否被改变或者根本不用。当我期望得到0或1时,我得到的r值是-1。

Here is my stored procedure:

以下是我的存储过程:

    USE [DATABASE_CS]
    GO

    SET ANSI_NULLS ON
    GO

    SET QUOTED_IDENTIFIER ON
    GO

    ALTER PROCEDURE [dbo].[proc_CsStatus]
        -- Add the parameters for the stored procedure here
        @TimeLimit Int,
        @Status Int OUTPUT
    AS
    BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON

        -- Declare variables.
        DECLARE @LastUpdate Int

        -- Calculate the LastUpdate.
        SELECT @LastUpdate = DATEDIFF(second, Timestamp, CURRENT_TIMESTAMP)
        FROM Heartbeat
        WHERE Id=1

        -- Compare it to the TimeLimit.
        IF @LastUpdate > @TimeLimit SELECT @Status = 0
        ELSE SELECT @Status = 1
    END
    GO

Any input is much appreciated!!!

非常感谢您的输入!!

1 个解决方案

#1


4  

After executing your procedure, your the ObjectParameter s will contain the value. Your procedure call will not return it. The value you are looking for should be able to be found in s.Value.

在执行过程之后,ObjectParameter s将包含该值。您的过程调用不会返回它。您正在寻找的值应该能够在. value中找到。

Try the following:

试试以下:

public static int GetCsStatus()
{
    using (Entities db = new Entities())
    {
        System.Data.Objects.ObjectParameter s = new System.Data.Objects.ObjectParameter("Status", typeof(int));
        int r = db.proc_CsStatus(120, s);
        return (int)s.Value;
    }
}

The value which you are returning(r) is the number of rows affected by your procedure.

返回的值(r)是受过程影响的行数。

More Info:

更多信息:

Behind the scenes, your procedure is doing something along the lines of the following:

在幕后,你的程序正在按照以下路线进行:

return base.ExecuteFunction("proc_CsStatus", input, output);

ObjectContext.ExecuteFunction() returns the number of rows affected by the call.

executefunction()返回受调用影响的行数。

#1


4  

After executing your procedure, your the ObjectParameter s will contain the value. Your procedure call will not return it. The value you are looking for should be able to be found in s.Value.

在执行过程之后,ObjectParameter s将包含该值。您的过程调用不会返回它。您正在寻找的值应该能够在. value中找到。

Try the following:

试试以下:

public static int GetCsStatus()
{
    using (Entities db = new Entities())
    {
        System.Data.Objects.ObjectParameter s = new System.Data.Objects.ObjectParameter("Status", typeof(int));
        int r = db.proc_CsStatus(120, s);
        return (int)s.Value;
    }
}

The value which you are returning(r) is the number of rows affected by your procedure.

返回的值(r)是受过程影响的行数。

More Info:

更多信息:

Behind the scenes, your procedure is doing something along the lines of the following:

在幕后,你的程序正在按照以下路线进行:

return base.ExecuteFunction("proc_CsStatus", input, output);

ObjectContext.ExecuteFunction() returns the number of rows affected by the call.

executefunction()返回受调用影响的行数。