存储过程分页:
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 tb2 10 where tb2.xh between (@pageIndex-1)*@pageSize+1 and @pageIndex*@pageSize 11 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 UseStoredProcedure 10 { 11 class Program 12 { 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通用完整分页
时间: 2024-10-07 20:51:59