实现查询与存储过程编译转换

使用标准查询可方便将查询编译为存储过程,以后再查询时会匹配对应的存储过程进行调用

方法原型

 1 /// <summary>
 2         /// 使用lamada设置条件查询
 3         /// </summary>
 4         /// <typeparam name="TItem"></typeparam>
 5         /// <param name="expression"></param>
 6         /// <param name="compileSp">是否编译成储过程</param>
 7         /// <returns></returns>
 8         public List<TItem> QueryList<TItem>(Expression<Func<TItem, bool>> expression =null, bool compileSp = false) where TItem : IModelBase, new()
 9         {
10             LamadaQuery<TItem> query = new LamadaQuery<TItem>();
11             query = query.Select(b => b.SelectField()).Where(expression);
12             string key;
13             return QueryList<TItem>(query, 0, out key, compileSp);
14         }

调用:

public List<ProductReceiptDetail> QueryDetail(string batchNo)
        {
            var helper = dbHelper;
            var list = helper.QueryList<ProductReceiptDetail>(b => b.BatchNo == batchNo,compileSp:true);
            return list;
        }

编译转换后的存储过程类似如下:

1  CREATE PROCEDURE [dbo].ZautoSp_1114C09A54B9CFAF
2 (@0AccountId nvarchar(500),@1TradeType nvarchar(500),@3Hash nvarchar(500))
3 AS
4 set  nocount  on
5     select top 1 t1.AccountId,t1.AddTime,t1.Amount,t1.CurrentBalance,t1.Hash,t1.Id,t1.LastBalance,t1.Name,t1.OperateType,t1.OutOrderId,t1.Remark,t1.TradeType,t1.TransactionNo,t1.TransactionType from [AccountTransaction] as t1 with (nolock)  where (((t1.AccountId=@0AccountId) AND (t1.TradeType=@1TradeType)) AND (t1.Hash=@3Hash)) order by  t1.Id desc

当然也可以用原生SQL直接转换:

public List<dynamic> GetSum(int storeId)
        {
            var helper = dbHelper;
            string sql = "select sum(TotalNum) Num,sum(OrderAmount) Amount,datepart(month,AddTime) Month from OrderMain where AddTime  between dateadd(month,datediff(month,-1,getdate())-7,-1) and dateadd(month,datediff(month,-1,getdate())-1,-1) and OwnerId = @storeId and [Status] = @status group by datepart(month,AddTime) ";
            helper.AddParam("storeId", storeId);
            helper.AddParam("Status", OrderStatus.已确认收货);
            return helper.AutoSpQueryDynamic(sql);
        }

实现查询与存储过程编译转换

时间: 2025-01-07 08:11:32

实现查询与存储过程编译转换的相关文章

获取多表联合查询的存储过程。

USE [NopV3.3Test] GO /****** Object: StoredProcedure [dbo].[GetPostList] Script Date: 06/18/2014 15:27:19 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: 于留洋 -- Create date: 2014/06

FMDB使用的一点心得:数据库创建、制表、查询等以及image转换成二进制nsdata保存到数据库中

<span style="font-size:18px;">//首先,获取数据库的路径,(如果不存在,就是我们想要存储的路径),不用担心每次都创建数据库会重复,不会的,如果没有则创建,有了就不会再重复创建: //接下来,我们要制表,首先我们先查看我们的表是否已经存在,方法在下面代码中:如果存在则跳过,不存在则创建</span> -(void)DataBaseInit { //获取Document文件夹下的数据库文件,没有则创建 NSString *dbPath

SQL Server查询所有存储过程信息、触发器、索引

1. [代码]查询所有存储过程     跳至 [1] [2] [3] [4] [5] [6] [全屏预览] ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 select Pr_Name as [存储过程], [参数]=stuff((select ','+[Parameter] from ( select Pr.Name as Pr_Name,parameter.name +' ' +Type.Name + ' ('+convert(varchar(3

C#通过OleDb查询Oracle存储过程

using System; using System.Data; using System.Data.OleDb; /// <summary> /// 通过OleDb查询Oracle存储过程 /// </summary> /// <param name="id"></param> /// <returns></returns> public static DataTable getTodoList(string i

正在编译转换: 未能找到元数据文件 EntityFramework.dll

错误 1 正在编译转换: 未能找到元数据文件“C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\..\IDE\EntityFramework.dll” 错误 2 正在编译转换: 未能找到元数据文件“C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\..\IDE\Microsoft.Data.Entity.Design.dll” 就是这

存储过程编译时会卡死

Oracle存储过程编译卡死 解决方法如下: 1:查V$DB_OBJECT_CACHE SELECT * FROM V$DB_OBJECT_CACHE WHERE name='CRM_LASTCHGINFO_DAY' AND LOCKS!='0'; 注意:CRM_LASTCHGINFO_DAY为存储过程的名称. 发现locks=2, 2:按对象查出sid的值 select /*+ rule*/  SID from V$ACCESS WHERE object='CRM_LASTCHGINFO_DA

自定义查询条件存储过程

create procedure pro_aaa (     @canshu1 nvarchar(200),     @canshu2 nvarchar(200) )  as  begin  declare @strsql nvarchar(2000)  set @strsql='select * from table1 where 1=1'[email protected]  exec(@strsql)  end 自定义查询条件存储过程

存储过程编译出现锁死情况的解决方法

本随笔文章,由个人博客(鸟不拉屎)转移至博客园 发布时间: 2018 年 10 月 23 日 原地址:https://niaobulashi.com/archives/procedures_locks.html --- 存储过程 先科普一下存储过程,在项目开发过程可能会遇到. 存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,存储在数据库中,经过第一次编译后再次调用不需要再次编译,用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)

SQLServer(子查询,存储过程,透视,索引)

存储过程: 视图是对一个查询语句的封装,而存储过程:将一段t-sql脚本进行封装,对一段逻辑操作的封装. 创建存储过程: create proc 名称 参数表 as begin end 案例:去左右空格 creat proc trim @str1 varchar(10)asbegin select LTRIM(RTRIM(@str1)) end 在<可编程性>里面有<存储过程>里面有trim这个名 用的时候: exec trim '参数' 案例:编写存储过程,查询表中的总数居,以及