日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

EF调用存储过程实现分页

發布時間:2023/12/10 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 EF调用存储过程实现分页 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


1、通用分頁代碼

public static List<T> SelectPageList<T>(string sqlstr, int pageIndex, int pagesize, string orderByField, ref int totalCount) where T : class

? ? {

? ? ? ? ? ?SqlParameter[] spm = new SqlParameter[5];

? ? ? ? ? ?spm[0] = new SqlParameter("@Sql", sqlstr);

? ? ? ? ? ?spm[1] = new SqlParameter("@PageIndex", pageIndex);

? ? ? ? ? ?spm[2] = new SqlParameter("@PageSize", pagesize);

? ? ? ? ? ?spm[3] = new SqlParameter("@OrderByField", orderByField);

? ? ? ? ? ?spm[4] = new SqlParameter("@TotalRecord", totalCount);

? ? ? ? ? ?spm[4].Direction = ParameterDirection.Output;? ? ? ? ??

? ? ? ? ?var data = db.Database.SqlQuery<T>("exec Pro_PageProcedure @Sql,@PageIndex,@PageSize,@OrderByField,@TotalRecord out", spm).ToList();

? ? ? ? ? ?totalCount = Convert.ToInt32(spm[4].Value.ToString());? ? ? ? ? ?

return data;

? ? ?}

2、通用的SQL分頁存儲過程

CREATE PROCEDURE [dbo].[Pro_PageProcedure]

?@Sql nvarchar(max),? ? ? ? --表名

?@PageIndex int = 1 ,? ? ? ? ? ? --指定當前為第幾頁

?@PageSize int,? ? ? ? ? ? ? ? ? ? --每頁多少條記錄

?@OrderByField nvarchar(1000),? ? ? ? --row_number需要的排序字段

?@TotalRecord int output? ? ? ? ? ?--返回總頁數?

as

? ? Declare @_sql nvarchar(max);? ??

? ? --計算總記錄數? ? ? ? ?

? ? set @_sql = 'select @TotalRecord = count(*) from (' + @Sql + ') a'

?

? ?EXEC sp_executesql @_sql,N'@TotalRecord int OUTPUT',@TotalRecord OUTPUT--計算總記錄數? ? ? ? ? ?

? ??

? ? Declare @StartRecord int

? ? Declare @EndRecord int

? ??

? ? set @StartRecord = (@pageIndex-1)*@PageSize + 1

? ? set @EndRecord = @StartRecord + @pageSize - 1

? ??

? ?set @_sql ='select * from ( select ROW_NUMBER() Over(' + @OrderByField + ') as _ttRowId,* from (' +? @Sql + ') _tt0 ) _tt1 '

? ?set @_sql = @_sql + 'where _ttRowId between ' + CAST(@StartRecord as nvarchar) + ' and ' + CAST(@EndRecord as nvarchar)

? ? ? ??

? ? Exec(@_sql)

3、調用示例

/// <summary>

? ? ? ? /// 獲取分頁列表

? ? ? ? /// </summary>

? ? ? ? /// <returns></returns>

? ? ? ? public List<FixedAnswer> GetPageList(int pageIndex, int pageSize, ref int totalCount)

? ? ? ? {? ? ? ? ? ??

? ? ? ? ? ? string strSql = "select * from FixedAnswer";

? ? ? ? ? ? string orderfied = "order by id desc";

? ? ? ? ? ? return SelectPageList<FixedAnswer>(strSql, pageIndex, pageSize, orderfied, ref totalCount);

? ? ? ? }


創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的EF调用存储过程实现分页的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。