码迷,mamicode.com
首页 > 其他好文 > 详细

EntityFramework 4使用存储过程分页

时间:2014-05-30 23:43:33      阅读:516      评论:0      收藏:0      [点我收藏+]

标签:des   c   style   class   blog   code   

bubuko.com,布布扣
 1 CREATE PROC usp_OrgPage_SQL
 2 @pageIndex INT,
 3 @pageSize INT,
 4 @totalCount INT OUTPUT
 5 AS
 6     BEGIN
 7         SET @totalCount = (SELECT COUNT(*) FROM dbo.Organization)
 8         SELECT * FROM
 9         (
10         SELECT *,ROW_NUMBER() OVER(ORDER BY OrganizationID DESC)AS row FROM dbo.Organization
11         )AS res
12         WHERE res.row BETWEEN @pageSize * (@pageIndex - 1) + 1 AND @pageSize * @pageIndex
13     END
bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣
 1 public List<OrganizationModel> SelectPageList(int pageIndex, int pageSize, out int totalCount)
 2         {
 3             var cmd = DbContext.Connection.CreateCommand();
 4             cmd.CommandText = "kydContext.usp_OrgPage_SQL";
 5             cmd.CommandType = System.Data.CommandType.StoredProcedure;
 6             cmd.Parameters.Add(new EntityParameter
 7             {
 8                 ParameterName = "pageIndex",
 9                 Value = pageIndex
10             });
11             cmd.Parameters.Add(new EntityParameter
12             {
13                 ParameterName = "pageSize",
14                 Value = pageSize
15             });
16             cmd.Parameters.Add(new EntityParameter
17             {
18                 ParameterName = "totalCount",
19                 Direction = System.Data.ParameterDirection.Output
20             });
21 
22             cmd.Connection.Open();
23             DbDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess);
24             List<OrganizationModel> organizationModelList = new List<OrganizationModel>();
25             while (reader.Read())
26             {
27                 OrganizationModel organizationModel = new OrganizationModel();
28                 organizationModel.OrganizationID = reader.GetInt32(reader.GetOrdinal("OrganizationID"));
29                 organizationModel.OrganizationName = reader.GetString(reader.GetOrdinal("OrganizationName"));
30                 organizationModelList.Add(organizationModel);
31             }
32             reader.Close();
33             totalCount = Convert.ToInt32(cmd.Parameters["totalCount"].Value);
34             cmd.Connection.Close();
35             return organizationModelList;
36         }
bubuko.com,布布扣

对于EntityFramework调用存储过程通常想到的是"Context.ExcuteFunction()"。

bubuko.com,布布扣

但是ExcuteFunction不返回任何结果,只有影响的行数,所以如果是通过存储过程实现分页及查询还得使用传统的command方式

EntityFramework 4使用存储过程分页,布布扣,bubuko.com

EntityFramework 4使用存储过程分页

标签:des   c   style   class   blog   code   

原文地址:http://www.cnblogs.com/tq1226112215/p/3761469.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!