c# - 使用 LINQ 访问存储过程值。 Entity Framework 5,数据库优先

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

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

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

    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

非常感谢您的任何意见!!!

最佳答案

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

关于c# - 使用 LINQ 访问存储过程值。 Entity Framework 5,数据库优先,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18752391/

相关文章:

c# - EF6 数据库优先 - EF 尝试创建我的数据库(但所有表都已存在)

c# - 将目标框架从 .NetStandard 2.0 更改为 .net 4.7.2 后无法进行 nuget 恢复

c# - 如何在 xml 数据的 linq 查询中使用 TryParse?

c# - 通过搜索过滤器(如日期)读取 Google 邮箱

c# - 将许多文本文件读入 SQL Server Express

SQL 使用值序列更新表列

sql-server - 逗号分隔列表作为单个字符串,T-SQL

sql-server - SQL Server : Multiple Where Clauses

c# Expression Lambda 从字符串到 Func<T>

.net - 通用存储库。需要建议