大数据量的数据分页1-视图

碰到需要分页的工作了,不让往Sql服务器上写存储过程,而且数据库字段也没有连续好用的列,那么问题来了,怎样才能舒坦的分页呢?

主要用到

ROW_NUMBER() over (order by AgentID desc) rows1

 思路如下:

1.把数据全部缓存到本地,然后在本地实现分页----不到两秒 Pass~

2.在服务器上写存储过程,-----Pass

3.直接用 between关键字,但是这样分出来的数据可能会乱!(每次查询向后走一百条,只取需要的10条,这只是一个假分页!)

4.视图啊!MS Sqlserver 中的视图啊!,详解如下:

1.窗体运行的时候检测数据库中需要的视图是否存在,不存在则创建。

 public bool ExistView()  //判断视图是否存在
        {
            string sql = "select count(*) from sysobjects where xtype='V' and name='StoreMainView' ";
            if (int.Parse(DB.Dbs.Retstr(sql)) > 0)
            {
 
                return true;
            }
            else
            {
                return false;
            }
        }
        public void CreateView()  //不存在则创建
        {
            string sql = "create view [MainView] as select ROW_NUMBER() over (order by AgentID desc) rows1  , *  from [QcAps_StoreMain]";
            SqlCommand comm = new SqlCommand();
            comm.CommandText = sql;
            DB.Dbs.RetCount(comm);
        }

创建完成视图之后,我们就可以用多出来的那一列【rows1】 来进行查询(做视图的根本就是为了有一个连续的列进行分页)

 public DataTable SelectView(int Start, int End)
        {
            string sql = string.Format("SELECT  [BankBranch] as '开户支行'  FROM [StoreMainView] WHERE [StoreID] between {0} and {1}", Start, End);
            return DB.Dbs.RetDt(sql);
        }

使用起来是这样的:

   private int PageCount = 0;//一共有多少页
        private int PageRows = 21; //一页有多少行
        private int RowsStart = 0;//起始行数
        private int RowsEnd = 0;//终止行数
        private int Inum = 0;//当前页数
        private int state = 0;  //1 = 代理主表,2 = 代理员工表 , 3 = 店铺表
        private void Empty()
        {
            PageCount = 0;
            PageRows = 21;
            RowsStart = 0;
            RowsEnd = 0;
            Inum = 0;
            PageSub.Enabled = true;
            PageAdd.Enabled = true;
        }  //清空分页数据
        private void AgentMainPage()
        {
            Empty();
            state = 1;
            int count = agentmain.GetAllCount();
            PageCount = count / PageRows;
            if (PageCount < 1)
            {
                PageCount = 1;
            }
            RowsEnd = RowsStart + PageRows;
            PubList.DataSource = agentmain.SelectView(RowsStart, RowsEnd);
            Inum++;
        }   //初始化分页

猜你喜欢

转载自blog.csdn.net/kone0611/article/details/88904928
今日推荐