使用EntityFramework调用存储过程并获取存储过程返回的结果集
【实习】刚入职,公司要求完成两个任务,任务要求使用存储过程和事务,其中一个问题要获取存储过程的查询结果集。经过多方查找和自己的实践,终于找到了方法。这里记录一下。
看到的这篇文章中给出的例子是查询单个表的所有数据,但是我的要求是多表查询并获取其中的几个字段。而且我是使用POCO(POCO是指Plain Old Class Object,也就是最基本的CLRClass),还是和示例有诸多不同。这里我结合文章中的示例和自己的尝试,解决了这个问题
首先建立存储过程 可以看到 这里我只需要sn.jewelry_type ,tr.[History Sign2] ,SUM(his.[Actual_Selling_Price]) AS Summary这三个字段
CREATE PROCEDURE dbo.SP_Branch_Month_Sells
@branchCode INT,
@transactionDate DATE
AS
SELECT
sn.jewelry_type AS JewelryType,
tr.[History Sign2] AS HistorySign2,
SUM(his.[Actual_Selling_Price]) AS Summary FROM dbo.History his
INNER JOIN dbo.[stock nature] sn
ON his.[Stock Type] = sn.stock_type
AND his.[Stock Group] = sn.stock_group
INNER JOIN dbo.[History Tran Code] tr
ON his.[Tran Code] = tr.[History Tran Code]
WHERE his.[Branch Code From] = @branchCode
AND sn.[jewelry_type] IN ( 1, 2, 3 )
AND tr.[History Sign2] IN ( 2, 7, 8, -2, -7, -8 )
AND DateDiff(mm,his.[Transaction Date],@transactionDate)=0
GROUP BY sn.jewelry_type, tr.[History Sign2]
GO
然后根据这三个字段建立对应的实体类和配置类,虽然在数据库中并没有对应的表,这里的实体类只是作为存储过程的结果集的对应实体
public class SellEntity
{
public Int16 JewelryType { get; set; }
public Int16 HistorySign2 { get; set; }
public decimal Summary { get; set; }
} public class SellConfig:EntityTypeConfiguration<SellEntity>
{
public SellConfig()
{
//EF 需要每个表都有一个键,这里的JewelryType并没有实际键的意义,只是为满足EF的要求
HasKey(u => u.JewelryType);
Property(u => u.JewelryType).HasColumnName("JewelryType");
Property(u => u.HistorySign2).HasColumnName("HistorySign2"); }
}
然后就可以使用EF调用存储过程来获取结果集并和实体类对应起来,注意这里我使用了DTO,
public class SellDTO
{
public int JewelryType { get; set; }
public int HistorySign2 { get; set; }
public decimal Summary { get; set; }
} public SellDTO[] GetSells(int branchCode, DateTime monthDate)
{
using(TrainContext ctx = new TrainContext())
{
var paramters = new SqlParameter[]
{
new SqlParameter("branchCode",SqlDbType.Int)
{ Value=branchCode},
new SqlParameter("transactionDate", SqlDbType.Date)
{
Value =monthDate.ToShortDateString()
}
};
//EO转换成DTO
return ctx.Sells.SqlQuery("EXEC dbo.SP_Branch_Month_Sells @branchCode,@transactionDate)", paramters)
.ToList().Select(u=>new SellDTO()
{
JewelryType = u.JewelryType,
HistorySign2 = u.HistorySign2,
Summary = u.Summary
}).ToArray();
}
}
这里是DAL层的代码,BLL层只是对参数做转发
public class SellBLL
{
public SellDTO[] GetSellByMonthAndBranchCode(int branchCode, DateTime monthDate)
{
var sellDAL = new SellDAL();
return sellDAL.GetSells(branchCode, monthDate);
}
}
下面进行单元测试:
[TestClass]
public class SellTests
{
[TestMethod]
public void GetSellByMonthAndBranchCodeTest()
{
SellBLL sellBLL = new SellBLL(); var sells = sellBLL.GetSellByMonthAndBranchCode(, DateTime.Today.AddDays(-));
}
}
测试结果


拿到结果集 和直接在数据库查询的结果一致。
成功
使用EntityFramework调用存储过程并获取存储过程返回的结果集的更多相关文章
- EF中执行存储过程,获取output返回值
EF不能直接支持执行存储过程,于是使用转化成执行SQL语句的形式,却怎么也获取不到output的值,折腾的好久,终于解决了,分享下曲折的经历: public int AddVote(int title ...
- 连接sqlServer数据库&jpa调用存储过程Java获取存储过程返回的多个结果集JAVA调用sqlserver存储过程的实现(返回多个结果集的实现)jdbc多结果集(getMoreResults)
存储过程: BEGIN select * from teacher; SELECT * FROM student; END public Object GetMyBOProjectProductLis ...
- [Java]在xp系统下java调用wmic命令获取窗口返回信息无反应(阻塞)的解决方案
背景:本人写了一段java代码,调用cmd命令“wmic ...”来获取系统cpu.mem.handle等资源信息.在win7操作系统下运行没有问题,在xp系统下却发现读取窗口反馈信息时无反应(阻塞) ...
- Shell $?获取函数返回值或者上一个命令的退出状态
Shell $?获取函数返回值或者上一个命令的退出状态 来自:互联网 时间:2021-01-12 阅读:2 $? 是一个特殊变量,用来获取上一个命令的退出状态,或者上一个函数的返回值. 所谓退出状态, ...
- Yii2.0调用sql server存储过程并获取返回值
1.首先展示创建sql server存储过程的语句,创建一个简单的存储过程,测试用. SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE P ...
- C#获取存储过程返回值和输出参数值的方法
//转自网络,先留个底 1.获取Return返回值 //存储过程 //Create PROCEDURE MYSQL // @a int, // @b int //AS // return @a + @ ...
- C#调用SQL中的存储过程中有output参数,存储过程执行过程中返回信息
C#调用SQL中的存储过程中有output参数,类型是字符型的时候一定要指定参数的长度.不然获取到的结果总是只有第一字符.本人就是由于这个原因,折腾了很久.在此记录一下,供大家以后参考! 例如: ...
- SqlDataReader 获取存储过程返回值
编写存储过程,获取不到返回值 附上代码: SqlDataReader reader = null;// totalRecords = ; try { SqlConnectionHolder conne ...
- SQL server 存储过程 C#调用Windows CMD命令并返回输出结果 Mysql删除重复数据保留最小的id C# 取字符串中间文本 取字符串左边 取字符串右边 C# JSON格式数据高级用法
create proc insertLog@Title nvarchar(50),@Contents nvarchar(max),@UserId int,@CreateTime datetimeasi ...
随机推荐
- git路径超长 及gitignore
1 忽略路径超长 git config --system core.longpaths true 2 比较全的gitignore https://www.gitignore.io/api/vim,no ...
- k8s+jenkins
1 server 的port , targetport, nodeport的区别 targetport为容器的暴露端口,为最后端的端口 port可以理解为pod的端口,pod是容器的外层,该端口可以在 ...
- kwargs - Key words arguments in python function
This is a tutorial of how to use *args and **kwargs For defining the default value of arguments that ...
- 英语单词independent
来源——https://www.docker.com/products/docker-hub 回到顶部 Share and Collaborate with Docker Hub Docker Hub ...
- Jenkins镜像
https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json
- springboot整合hibernate案例
1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.GITHUB地址 https://github.com/nbfujx/springBo ...
- php substr_count()函数 语法
php substr_count()函数 语法 作用:统计一个字符串,在另一个字符串中出现次数大理石量具 语法:substr_count(string,substring,start,length) ...
- 【CF1255A】Changing Volume【思维】
题意:每次可以-5/-2/-1/+1/+2/+5,问是否存在方案使得A变成B 题解:首先我们可以设A<B,若A>B,则交换AB,因为A到B和B到A是互逆的过程,所以可以交换 其次将B-=A ...
- CF889 E Mod Mod Mod——DP
题目:http://codeforces.com/contest/889/problem/E 这题真好玩. 官方题解说得很好. 想到相邻 a[ i ] 之间的段可能可以一起维护,但是不太会. 原来是表 ...
- 解决在vue_cli上使用mui或引入mui.js各种报错及问题
原文:https://blog.csdn.net/u012815877/article/details/81187826 在main.js里添加 import mui from './assets/j ...