用代码说废话之--实现ADO.NET Entity Framework 的数据分页

//该分页类简单的实现了ADO.NET Entity Framework 的数据分页,你可以继续改进该代码

//作者:黄颢鹏,Email:[email protected],转载请注作者名

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Entity;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Data.Common;

namespace MyData
{
   public class 分页类
    {
         //作者:黄颢鹏,Email:[email protected],转载请注作者名

        //*分页代码
        private Boolean 上一页有效性1 = false;
        public Boolean 上一页有效性
        {
            set
            {
                this.上一页有效性1 = value;
            }
            get
            {
                return this.上一页有效性1;
            }
        }
        private Boolean 下一页有效性1 = false;
        public Boolean 下一页有效性
        {
            set
            {
                this.下一页有效性1 = value;
            }
            get
            {
                return this.下一页有效性1;
            }
        }
        private int 上一页跳过数1 = 0;
        public int 上一页跳过数
        {
            set
            {
                this.上一页跳过数1 = value;
            }
            get
            {
                return this.上一页跳过数1;
            }
        }
        private int 下一页跳过数1 = 0;
        public int 下一页跳过数
        {
            set
            {
                this.下一页跳过数1 = value;
            }
            get
            {
                return this.下一页跳过数1;
            }
        }
        private int 记录总数1 = 0;
        public int 记录总数
        {
            set
            {
                this.记录总数1 = value;
            }
            get
            {
                return this.记录总数1;
            }
        }
        private int 总页数1 = 0;
        public int 总页数
        {
            set
            {
                this.总页数1 = value;
            }
            get
            {
                return this.总页数1;
            }
        }
        private int 每页记录数1 = 25;
        public int 每页记录数
        {
            set
            {
                this.每页记录数1 = value;
            }
            get
            {
                return this.每页记录数1;
            }
        }
        private int 当前页1 = 0;
        public int 当前页
        {
            set
            {
                this.当前页1 = value;
            }
            get
            {
                return this.当前页1;
            }
        }
        private string 查询SQL1 = string.Empty;
        public string 查询SQL
        {
            set
            {
                this.查询SQL1 = value;
            }
            get
            {
                return this.查询SQL1;
            }
        }
        //以下为操作函数
        public void 获得总页数()
        {
            this.总页数 = 0;
            this.当前页 = 0;
            this.记录总数 = 0;

            string esql1 = this.查询SQL;
            xingshaisystemEntities db = new xingshaisystemEntities();
            string esql2 = esql1.Replace("select c.[医院名]", "select value count(c.[医院名]) ");
            ObjectQuery query = db.CreateQuery(esql2);
            this.记录总数 = Convert.ToInt16(query.First().ToString());
            int numok = Convert.ToInt16(this.记录总数 / this.每页记录数);

            if ((numok * this.每页记录数) == this.记录总数)
            {
                this.总页数 = numok;
            }
            else
            {
                this.总页数 = numok + 1;
            }
            下一页有效性 = false;
            下一页有效性 = false;
        }
        public BindingSource 第一页数据源()
        {
            string 分页sql = string.Empty;
            this.当前页 = 1;
            上一页跳过数 = 0;
            if (this.总页数 != 1 && this.总页数 != 0)
            {
                分页sql = " order by c.[拼音码] skip 0 limit " + this.每页记录数.ToString();
                下一页跳过数 = 每页记录数;
                下一页有效性 = true;
                上一页有效性 = false;
            }
            else
            {
                下一页跳过数 = 0;//只有一页
                下一页有效性 = false;
                下一页有效性 = false;

            }

            string esql1 = this.查询SQL + 分页sql;

            BindingSource bindingSource1 = new BindingSource();
            xingshaisystemEntities db = new xingshaisystemEntities();
            ObjectQuery query2 = db.CreateQuery(esql1);
            bindingSource1.DataSource = query2;
            return bindingSource1;
        }

        private int 按键次数 = 0;
        public BindingSource 上一页数据源()
        {

            string 分页sql = string.Empty;

            BindingSource bindingSource1 = new BindingSource();
            if (当前页 > 1)
            {
                下一页有效性 = true;
                if (按键次数 == 0)
                {
                    上一页跳过数 = 下一页跳过数 - this.每页记录数 * 2;
                }
                else
                {
                    上一页跳过数 = 下一页跳过数 - this.每页记录数;
                }
                下一页跳过数 = 下一页跳过数 - this.每页记录数;
                当前页--;
                if (当前页 == 1)
                {
                    上一页有效性 = false;
                }

                分页sql = "  order by c.[拼音码] skip " + 上一页跳过数.ToString() + " limit " + this.每页记录数.ToString();

                string esql1 = this.查询SQL + 分页sql;


                xingshaisystemEntities db = new xingshaisystemEntities();
                ObjectQuery query2 = db.CreateQuery(esql1);
                bindingSource1.DataSource = query2;

            }
            return bindingSource1;
        }
        public BindingSource 下一页数据源()
        {
            按键次数 = 0;
            string 分页sql = string.Empty;

            BindingSource bindingSource1 = new BindingSource();
            if (this.总页数 != 1 && this.总页数 != 0)
            {
                上一页有效性 = true;
                分页sql = "  order by c.[拼音码] skip " + 下一页跳过数.ToString() + " limit " + this.每页记录数.ToString();


                string esql1 = this.查询SQL + 分页sql;

                当前页++;

                if (当前页 == 总页数)
                {
                    下一页有效性 = false;
                }
                xingshaisystemEntities db = new xingshaisystemEntities();
                ObjectQuery query2 = db.CreateQuery(esql1);
                bindingSource1.DataSource = query2;
                下一页跳过数 = 下一页跳过数 + this.每页记录数;
            }
            return bindingSource1;
        }

        public BindingSource 翻页数据源(int 翻页)
        {
            string 分页sql = string.Empty;
            当前页 = 翻页;
            BindingSource bindingSource1 = new BindingSource();
            if (总页数>1)
            {
               
                分页sql = "  order by c.[拼音码] skip " + ((翻页-1) * this.每页记录数).ToString() + " limit " + this.每页记录数.ToString();

                string esql1 = this.查询SQL + 分页sql;


                xingshaisystemEntities db = new xingshaisystemEntities();
                ObjectQuery query2 = db.CreateQuery(esql1);
                bindingSource1.DataSource = query2;
                if (翻页 == 总页数)
                {
                    下一页有效性 = false;
                }
                else
                {
                    下一页跳过数 = (翻页 - 1) * this.每页记录数 + this.每页记录数;
                    下一页有效性 = true;
                }
                if (翻页 == 1)
                {

                    上一页有效性 = false;
                }
                else
                {
                    上一页跳过数 = (翻页 - 1) * this.每页记录数 - this.每页记录数;
                    上一页有效性 = true;
                }
            }
            return bindingSource1;
        }
        //*分页代码
      
    }
}

 

//如何使用分页类代码
 public MyData.分页类<你要查询的类> db = new 分页类<你要查询的类>();
        public DataGridView dataGridView2;
        public void 查询(ref DataGridView dataGridView1)
        {
            db.查询SQL = this.查询SQL;
            db.获得总页数();
            dataGridView1.DataSource = db.第一页数据源();
            page_num.Text = db.每页记录数.ToString() + "/" + db.总页数.ToString();
            当前页.Text = db.当前页.ToString();
            this.num.Text = db.记录总数.ToString();
            if (!db.下一页有效性)
            {
                this.下一页.Enabled = false;
            }
            else
            {
                this.下一页.Enabled = true;
            }
            if (!db.上一页有效性)
            {
                this.上一页.Enabled = false;
            }
            else
            {
                this.上一页.Enabled = true;
            }
            dataGridView2 = dataGridView1;


            翻页.Items.Clear();
            if (this.下一页.Enabled)
            {
                for (int i = 1; i                 {
                    翻页.Items.Add(i);
                }
            }
        }
    
        private void 下一页_Click(object sender, EventArgs e)
        {
            dataGridView2.DataSource =  db.下一页数据源();
            当前页.Text = db.当前页.ToString();
            if (!db.下一页有效性)
            {
                this.下一页.Enabled = false;
            }
            this.上一页.Enabled = true;
        }

        private void 上一页_Click(object sender, EventArgs e)
        {
            dataGridView2.DataSource = db.上一页数据源();
            当前页.Text = db.当前页.ToString();
            if (!db.上一页有效性)
            {
                this.上一页.Enabled = false;
            }
            this.下一页.Enabled = true;
        }


        private void 翻页_SelectedIndexChanged(object sender, EventArgs e)
        {

            dataGridView2.DataSource = db.翻页数据源(Convert.ToInt16(翻页.Text.Trim()));
            当前页.Text = db.当前页.ToString();
            if (!db.下一页有效性)
            {
                this.下一页.Enabled = false;
            }
            else
            {
                this.下一页.Enabled = true;
            }
            if (!db.上一页有效性)
            {
                this.上一页.Enabled = false;
            }
            else
            {
                this.上一页.Enabled = true;
            }
        }

你可能感兴趣的:(用代码说废话之--实现ADO.NET Entity Framework 的数据分页)