預存程序分頁 Ado.Net分頁 EF分頁 滿足90%以上

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   sp   div   

預存程序分頁:

 1 create proc PR_PagerDataByTop 2 @pageIndex int, 3 @pageSize int, 4 @count int out 5 as 6 select top(@pageSize) * from dbo.userInfo where ID not in 7 ( 8     select top((@pageIndex-1)*@pageSize) ID from dbo.userInfo 9 )10 set @count = (select COUNT(1) from dbo.userInfo)
top pager
 1 alter proc PR_PagerDataByRow 2 @pageIndex int, 3 @pageSize int, 4 @count int out 5 as 6 select * from  7 ( 8     select *,ROW_NUMBER() over (order by id) as xh from dbo.userInfo  9 )as tb210 where tb2.xh between (@pageIndex-1)*@pageSize+1 and @pageIndex*@pageSize11 set @count = (select COUNT(1) from dbo.userInfo)
row_number pager

Ado.Net分頁:

這裡只上傳一個例子(同理:可以調用上面2個預存程序):

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5  6 using System.Data; 7 using System.Data.SqlClient; 8  9 namespace UseStoredProcedure10 {11     class Program12     {13         static void Main(string[] args)14         {15             string conStr = "server=.;database=MyDb;uid=sa;pwd=123";16             using (SqlConnection conn = new SqlConnection(conStr))17             {18                 using (SqlCommand cmd = new SqlCommand())19                 {20                     cmd.Connection = conn;21                     cmd.CommandType = CommandType.StoredProcedure;22                     cmd.CommandText = "PR_PagerData";23                     cmd.Parameters.Add("@pageSize", 12);24                     cmd.Parameters.Add("@pageIndex", 1);25                     cmd.Parameters.Add("@count", 1).Direction = ParameterDirection.Output;26                     using (SqlDataAdapter da = new SqlDataAdapter(cmd))27                     {28                         conn.Open();29                         DataSet ds = new DataSet();30                         da.Fill(ds);31                         int count = (int)cmd.Parameters["@count"].Value;32                     }33                 }34             }35         }36     }37 }
調用預存程序

EF分頁:

Linq(EF實際也是調用Ado.Net):

1         public IQueryable<T> GetPageUserInfos<s>(int pageIndex, int pageSize, Func<T, bool> whereLmd, Func<T, s> orderLmd, out int count, bool isAsc)2         {3             var set = db.Set<T>();4             count = set.Count();5             return set.Where(whereLmd).OrderBy(orderLmd).6                 Skip(pageSize * (pageIndex - 1)).Take(pageSize).AsQueryable();7         }
Linq通用完整分頁

預存程序分頁 Ado.Net分頁 EF分頁 滿足90%以上

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.