【问题标题】:Access stored procedure value with LINQ. Entity Framework 5, DB first使用 LINQ 访问存储过程值。实体框架 5,数据库优先
【发布时间】:2023-03-06 01:58:01
【问题描述】:

我的存储过程工作正常。但是,我无法检索它。

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

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

我不介意这是否已更改或根本不使用。当我期待 0 或 1 时,我目前得到的“r”值为 -1。

这是我的存储过程:

    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

任何意见都非常感谢!!!

【问题讨论】:

    标签: c# sql-server linq entity-framework stored-procedures


    【解决方案1】:

    执行您的程序后,您的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() 返回受调用影响的行数。

    【讨论】:

    • 我得到一个红色的大字报,上面写着“错误 2 'int' 不包含 'Value' 的定义,并且找不到接受 'int' 类型的第一个参数的扩展方法 'Value' (您是否缺少 using 指令或程序集引用?)
    • 你可能只需要return s,而不是s.Value
    • 您的两个陈述相互矛盾。您在第一个错误中说s 是上面的int,然后在后者中说它是ObjectParameter。如果是ObjectParameter,应该可以使用s.Value,因为ObjectParameter的属性是Value
    • 如果我不得不猜测的话。我假设您第一次尝试返回 r.Value
    • 我刚刚复制了你的环境。我必须做的唯一改变是我需要将s.Value 转换为int。我已经编辑了我的答案以反映它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多