2

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.

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!!!

4

1 回答 1

4

执行您的程序后,您的ObjectParameter s将包含该值。您的过程调用不会返回它。您要查找的值应该可以在s.Value.

尝试以下操作:

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;
    }
}

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

更多信息:

在幕后,您的过程正在执行以下操作:

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

ObjectContext.ExecuteFunction()返回受调用影响的行数。

于 2013-09-11T22:49:05.203 回答