简体   繁体   English

如何从存储过程中获取输出结果在C#中使用Entity Framework?

[英]How to get output result from stored procedure use Entity Framework in C#?

I'm working on an ASP.NET MVC project; 我正在开发一个ASP.NET MVC项目; my goal is to prepare a report from a table so, the first time I've wrote Linq code but it was too slow. 我的目标是从表中准备一份报告,这是我第一次写Linq代码但是它太慢了。

And after that I've written a SQL query it was so fast and I want to use stored procedure for getting report data from my table. 之后我编写了一个SQL查询,它的速度非常快,我想使用存储过程从我的表中获取报表数据。 In fact my project is so easy: it gets two dates - start date and end date - and displays result in a table. 事实上,我的项目非常简单:它有两个日期 - 开始日期和结束日期 - 并在表格中显示结果。

I want to write my stored procedure to get two parameters - start date and end date - from C# code and then return output in a variable in C#. 我想编写我的存储过程以从C#代码获取两个参数 - 开始日期和结束日期,然后在C#中的变量中返回输出。

The first question: how to convert my SQL query to a stored procedure with two parameters, start Date and End date? 第一个问题:如何将我的SQL查询转换为具有两个参数的存储过程,开始日期和结束日期?

The second question: how to return output result in C#? 第二个问题:如何在C#中返回输出结果?

SELECT 
    CAST(date_rec_slash AS DATETIME), COUNT(code_marz) AS total,
    CASE
       WHEN code_marz = 1 THEN 'a'
       WHEN code_marz = 2 THEN 'b'
       WHEN code_marz = 3 THEN 'c'
       WHEN code_marz = 4 THEN 'd'
       WHEN code_marz = 5 THEN 'e'
    END
FROM 
    dbo.tbl_bar 
WHERE 
    CAST(date_rec_slash AS DATETIME) BETWEEN '2017/12/01' AND '2017/12/31'
GROUP BY 
    CAST(date_rec_slash AS DATETIME), code_marz
ORDER BY 
    CAST(date_rec_slash AS DATETIME) ASC;

C#: C#:

var spResults = db.Database.SqlQuery<tbl_bar>("Report");

在此输入图像描述

Declare your store procedure using this syntax: 使用以下语法声明您的商店过程:

    USE yourDataBaseNAme
    GO

    CREATE PROCEDURE [dbo].yourStoreProcedureName
    @startDate nvarchar(30), 
    @endDate   nvarchar(30)
    AS 
       SELECT Cast(date_rec_slash as datetime) AS 'date_rec_slash', count(code_marz) as total,
              CASE
                  WHEN code_marz = 1 THEN 'a'
                  WHEN code_marz = 2 THEN 'b'
                  WHEN code_marz = 3 THEN 'c'
                  WHEN code_marz = 4 THEN 'd'
                  WHEN code_marz = 5 THEN 'e'
              END AS 'code_marz'
      FROM dbo.tbl_bar 
     WHERE Cast(date_rec_slash as datetime) between @startDate 
                                                AND @endDate
     GROUP BY Cast(date_rec_slash as datetime), code_marz
     ORDER BY Cast(date_rec_slash as datetime) ASC;
    GO

Call this store procedure in EF: 在EF中调用此存储过程:

db.Database.SqlQuery<yourObjectNameToCAST>("yourStoreProcedureName");

Call store procedure with parameter in EF: 使用EF中的参数调用存储过程:

SqlParameter startDate= new SqlParameter("@startDate", "Value");
SqlParameter endDate= new SqlParameter("@endDate", "Value");
db.Database.SqlQuery<yourObjectNameToCAST>("exec yourStoreProcedureName @startDate, @endDate", startDate, endDate).ToList();

your Object to Cast: 你的对象:

public class yourObjectNameToCAST
{
     public datetime date_rec_slash { get; set; }
     public int total { get; set; }
     public string code_marz { get; set; }
}

You can declare your stored procedures using 您可以使用声明存储过程

CREATE PROCEDURE [dbo].YourStoredProcedure
    @Start DATETIME, 
    @END   DATETIME
AS

Then you can get rid of the code needed to cast from string 然后你可以摆脱从字符串转换所需的代码

In order to get the results mapped as ac# object, you need to use SqlQuery or FromSql depending on the version of Entity Framework that you are using 为了将结果映射为ac#对象,您需要使用SqlQueryFromSql具体取决于您使用的Entity Framework的版本

Entity Framework 6 实体框架6

var result = dbContext.Bar.SqlQuery("EXEC YourStoredProcedure").ToList();

To pass a parameter, you woild do something like 要传递参数,你可以做类似的事情

var result = dbContext.Bar.SqlQuery("EXEC YourStoredProcedure @SomeParameter", 
             new SqlParameter("@SomeParameter", TheParameterValue)).ToList();

And for Entity Framework Core 2 对于Entity Framework Core 2

var result = dbContext.Bar
.FromSql("EXEC YourStoredProcedure")
.ToList();

Where Bar is your C# object declared as a property with type DbSet<Bar> in your dbContext class 其中Bar是您的C#对象,在dbContext类中声明为类型为DbSet<Bar>的属性

Based on your output you should create a C# object similar to 根据您的输出,您应该创建一个类似于的C#对象

public class Bar
{
     public datetime DateRecSlash { get; set; }
     public int CodeMarz { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用实体框架从 C# 中的存储过程中获取输出值 - How can I get output value from the stored procedure in C# using Entity Framework 如何使用实体框架将mysql存储过程结果集设置为C# - How to get mysql stored procedure result set to C# with entity framework 如何在实体框架中使用“从存储过程中选择”结果 - How use “Select” Result from Stored procedure in Entity FrameWork 如何首先从实体框架+数据库中的存储过程中获取结果 - how to get the result from stored procedure in entity framework + database first 如何使用实体框架执行存储过程并从存储过程获取输出 - How to execute stored procedure and get output from the stored procedure using Entity Framework C#+从存储过程读取输出结果+合并到+ OUTPUT - C# + Reading output result from stored procedure + merge into + OUTPUT 在C#中从实体框架调用存储过程 - Calling stored procedure from Entity Framework in C# 实体框架存储过程 - c# , Entity Framework Stored Procedure 如何使用实体框架从Webform中的存储过程获取exec(query)结果? - How to get exec(query) result from stored procedure in webform using entity framework? C# Entity Framework Core:如何执行存储过程并获取自定义数据列表? - C# Entity Framework Core : how to execute stored procedure and get a list of custom data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM