步步为营-46-分页显示的SQL语句

Posted 逍遥小天狼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了步步为营-46-分页显示的SQL语句相关的知识,希望对你有一定的参考价值。

说明:分页显示在实际业务中经常需要用到,其SQL语句分两种

1:分页显示SQL语句

--方法一:跳过多少行,选中多少行
--每页n条,选择第m页--n=2 m=3
--select top(n) * fromwhere 主键 not in (select top(m-1)*n 主键 from 表);
select  * from UserInfo
select top(2) * from UserInfo where Empid not in (select top((3-1)*2) EmpId from UserInfo);
--方法二,通过rowNumber函数,但是只能当作临时表
select * from(select * ,ROW_NUMBER() over (order by EmpId) as num from UserInfo) as T
where T.num between (3-1)*2+1 and 3*2;
--over开窗函数的的另一个用法
select top(2) * ,AVG(StuAge) over() as 平均年龄 from UserInfo;
View Code

 2:分页显示存储过程 

--03-01 判断存储过程是否存在,如果存在则进行删除
   if(exists(select * from sys.all_objects where name =\'usp_UserInfo_GetPageData\'))
        drop proc usp_UserInfo_GetPageData
    go

    --03-02 创建分页的存储过程
create procedure usp_UserInfo_GetPageData
    @PageSize int,
    @PageIndex int,
    @TotalCount int output
    as 
BEGIN
        select * from 
        (select * ,ROW_NUMBER() over (Order by EmpId) as rowNumber from UserInfo where Delflag = 0) as temp 
        where temp.rowNumber between (@PageSize*(@PageIndex-1)+1) and (@PageSize*@PageIndex)
        select @TotalCount =  count(1) from UserInfo where Delflag = 0
END
--03-02 调用存储过程
declare @TotalCount int
exec usp_UserInfo_GetPageData 2,3,@TotalCount out
select @TotalCount
View Code

3:在c#中实现
  3.1,发现输出参数没有什么用,修改存储过程

ALTER procedure [dbo].[usp_UserInfo_GetPageData]
    @PageSize int,
    @PageIndex int
    as 
BEGIN
        select * from 
        (select * ,ROW_NUMBER() over (Order by EmpId) as rowNumber from UserInfo where Delflag = 0) as temp 
        where temp.rowNumber between (@PageSize*(@PageIndex-1)+1) and (@PageSize*@PageIndex)
END
View Code

  3.2 调用代码  

        #region //06-06 跳到某一页---存储过程
        private void btnSkip_Click(object sender, EventArgs e)
        {
            //01-00 设置强类型数据源
            List<UserInfo> userInfoList = new List<UserInfo>();
            int pageIndex =int.Parse(txtSkipPage.Text);
            using (SqlConnection conn = new SqlConnection( ConnStr))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                     conn.Open();
                    cmd.CommandText = "usp_UserInfo_GetPageData";
                    cmd.Parameters.Add(new SqlParameter("@PageSize", pageSize));
                    cmd.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
                    cmd.CommandType = CommandType.StoredProcedure;
                  
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                                //数据封装
                                UserInfo userInfo = new UserInfo();
                                userInfo.EmpId = int.Parse(reader["EmpId"].ToString());
                                userInfo.Pwd = reader["Pwd"].ToString();
                                userInfo.StuName = reader["StuName"].ToString();
                                userInfo.StuAge = int.Parse(reader["StuAge"].ToString());
                                userInfo.Delflag = Char.Parse(reader["Delflag"].ToString());
                                userInfo.ClassNo = int.Parse(reader["ClassNo"] == DBNull.Value ? "-1" : reader["ClassNo"].ToString());
                                //添加到列表中
                                userInfoList.Add(userInfo);
                        }
                    }
                }
            }
            //01-06 配置数据源
            this.dataGridView1.DataSource = userInfoList;
         }
      
        #endregion
View Code

 

以上是关于步步为营-46-分页显示的SQL语句的主要内容,如果未能解决你的问题,请参考以下文章

SQL Select 语句的用法

如何用sql语句 实现分页查询

PHP——分页显示的完善(加查询,用类简化sql语句)

Java分页查询--分页显示

springmvc 分页查询的简单实现

PHP分页初探 一个最简单的PHP分页代码实现