首先建立一个存储过程如下(MySQL数据库):
CREATE DEFINER=`root`@`localhost` PROCEDURE `pagination`(
       in tbName varchar(100),   /*表名*/
          fldName varchar(100),  /*排序关键字*/
          pageSize int,          /*每页显示行数*/
          pageIndex int,         /*当前显示第几页*/
          orderType int,         /*排序规则 0升序,1降序*/
          strWhere varchar(2000),/*查询条件 例如where 1=1*/
       out allPages int          /*传出参数 返回总行数*/
)
begin

declare beginRow int;
     declare sqlStr varchar(1000);
     declare limitTemp varchar(1000);
     declare orderTemp varchar(1000);
     declare allPageNum int;
    
     set allPageNum = 0;
     set beginRow = (pageIndex-1)*pageSize;
     set sqlStr = CONCAT('SELECT * from ',tbName);
     set limitTemp = CONCAT(' limit ',beginRow,',',pageSize);
     set orderTemp = CONCAT(' order by ',fldName);
     if orderType = 0 then
         set orderTemp = CONCAT(orderTemp,' ASC ');
     else
         set orderTemp = CONCAT(orderTemp,' DESC ');
     end if;
    
     set @sqlSelect = CONCAT(sqlStr,' ',strWhere,orderTemp,limitTemp);
    
     set @sqlCountRow = CONCAT('select count(*) into @countRow from ',tbName);
    
     prepare sqlstmt from @sqlSelect;
     execute sqlstmt;
     deallocate prepare sqlstmt;
    
     prepare sqlstmt from @sqlCountRow;
     EXECUTE sqlstmt;
     set allPages = @countRow;
     deallocate prepare sqlstmt;

end;


建立一个排序类
using System;
using System.Data;
using MySql.Data.MySqlClient;

namespace TestMySQL
{
    public class Procedure
    {
        public Procedure()
        {
        }

/// <summary>
        /// 根据条件查询,返回一页记录
        /// </summary>
        /// <param name="tbName">要查询的表名  多个表之间用","分隔</param>
        /// <param name="fldName">排序关键字段</param>
        /// <param name="pageSize">每页显示行数</param>
        /// <param name="pageIndex">显示第几页</param>
        /// <param name="orderType">排序规则  0为升序,1为降序</param>
        /// <param name="strWhere">查询条件 例如"where 1=1'</param>
        /// <param name="allNum">返回总行数</param>
        /// <returns>一页的记录</returns>
        public static DataTable Query(string tbName,string fldName,int pageSize,int pageIndex,int orderType,string strWhere,out int allNum)
        {
           
//            MySqlConnection con = DB.CreateCon();
            MySqlConnection con = new MySqlConnection("server=localhost; database=testprocedure; user id=root; password=123456");
            MySqlCommand mySqlCmd = new MySqlCommand();
            mySqlCmd.Connection = con;
            mySqlCmd.CommandText = "pagination";
            mySqlCmd.CommandType = CommandType.StoredProcedure;

//添加参数列表
            mySqlCmd.Parameters.Add(new MySqlParameter("tbName",MySql.Data.MySqlClient.MySqlDbType.VarChar));
            mySqlCmd.Parameters["tbName"].Value = tbName;

mySqlCmd.Parameters.Add(new MySqlParameter("fldName",MySql.Data.MySqlClient.MySqlDbType.VarChar));
            mySqlCmd.Parameters["fldName"].Value = fldName;

mySqlCmd.Parameters.Add(new MySqlParameter("pageSize",MySql.Data.MySqlClient.MySqlDbType.Int32));
            mySqlCmd.Parameters["pageSize"].Value = pageSize;

mySqlCmd.Parameters.Add(new MySqlParameter("pageIndex",MySql.Data.MySqlClient.MySqlDbType.Int32));
            mySqlCmd.Parameters["pageIndex"].Value = pageIndex;

mySqlCmd.Parameters.Add(new MySqlParameter("orderType",MySql.Data.MySqlClient.MySqlDbType.Int32));
            mySqlCmd.Parameters["orderType"].Value = orderType;

mySqlCmd.Parameters.Add(new MySqlParameter("strWhere",MySql.Data.MySqlClient.MySqlDbType.VarChar));
            mySqlCmd.Parameters["strWhere"].Value = strWhere;

mySqlCmd.Parameters.Add(new MySqlParameter("allPages",MySql.Data.MySqlClient.MySqlDbType.Int32));
//            mySqlCmd.Parameters["@allNum"].Value = allNum;
            mySqlCmd.Parameters["allPages"].Direction = ParameterDirection.Output;

MySqlDataAdapter msda = new MySqlDataAdapter(mySqlCmd);            
            DataSet ds = new DataSet();
            msda.Fill(ds,"temp");
           
            allNum = Convert.ToInt32(mySqlCmd.Parameters["allPages"].Value);
           
            return ds.Tables["temp"];
        }
    }
}


使用排序类
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace TestMySQL
{
    public class WebForm3 : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.DataGrid DataGrid1;

private string tbName = "table1";//表名
        private string sortKey = "id";//排序关键字
        private int pageSize = 20;//每页显示行数
        private int CurrentPageIndex = 1;//当前页数
        private int sortRule = 0;//排序规则 0升,1降
        private string selectCondition = "where 1=1";//查询条件
        private int countRow;//返回的总行数
        private int countPage;//总页数
   
        private void Page_Load(object sender, System.EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                this.BingDataGird();
            }
        }

private void BingDataGird()
        {
            //排序关键字段 首次加载是按id排
            if(ViewState["sortKey"] == null)
            {
                ViewState["sortKey"] = "id";
            }
            sortKey = (string)ViewState["sortKey"];

//排序规则 0为升序,1为降序,首次加载时按升序
            if(ViewState["sortRule"] == null)
            {
                ViewState["sortRule"] = 0;
            }
            sortRule = (int)ViewState["sortRule"];

this.DataGrid1.PageSize = pageSize;//DataGird默认的每页显示行数等于自己设定的每页显示行数
            CurrentPageIndex = this.DataGrid1.CurrentPageIndex + 1;
            DataTable dt = Procedure.Query(tbName,sortKey,pageSize,CurrentPageIndex,sortRule,selectCondition,out countRow);

this.DataGrid1.VirtualItemCount = countRow;
            this.DataGrid1.DataSource = dt;
            this.DataGrid1.DataBind();

//计算总页数
            if(countRow%pageSize == 0)
            {
                countPage = countRow/pageSize;
            }
            else
            {
                countPage = countRow/pageSize +1;
            }
           
            Response.Write("总记录数为:"+countRow+"<br>"
            +"总页数为:"+countPage+"<br>"
            +"当前页数:"+CurrentPageIndex);
        }

private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
        {
            this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
            this.BingDataGird();
        }

private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
        {
            //设定双向排序
            if(ViewState["sortRule"] == null)
            {
                ViewState["sortRule"] = 0;
            }
            else
            {
                if( (int)ViewState["sortRule"] == 0)
                {
                    ViewState["sortRule"] = 1;
                }
                else
                {
                    ViewState["sortRule"] = 0;
                }
            }

sortRule = (int)ViewState["sortRule"];//排序规则
            this.DataGrid1.CurrentPageIndex = 0;//每次点击关键字排序时返回第一页

ViewState["sortKey"] = e.SortExpression;//排序关键字
            sortKey = (string)ViewState["sortKey"];

this.DataGrid1.PageSize = pageSize;//DataGird默认的每页显示行数等于自己设定的每页显示行数
            CurrentPageIndex = this.DataGrid1.CurrentPageIndex + 1;
            DataTable dt = Procedure.Query(tbName,sortKey,pageSize,CurrentPageIndex,sortRule,selectCondition,out countRow);

this.DataGrid1.VirtualItemCount = countRow;
            this.DataGrid1.DataSource = dt;
            this.DataGrid1.DataBind();

//计算总页数
            if(countRow%pageSize == 0)
            {
                countPage = countRow/pageSize;
            }
            else
            {
                countPage = countRow/pageSize +1;
            }
           
            Response.Write("总记录数为:"+countRow+"<br>"
                +"总页数为:"+countPage+"<br>"
                +"当前页数:"+CurrentPageIndex);
        }

private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            if(e.Item.ItemType == ListItemType.Item||e.Item.ItemType == ListItemType.AlternatingItem)
            {
                e.Item.Attributes.Add("onmouseover","c=this.style.backgroundColor,this.style.backgroundColor='#fff111'");
                e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=c");
            }
        }

#region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }
       
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {   
            this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
            this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid1_SortCommand);
            this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
            this.Load += new System.EventHandler(this.Page_Load);

}
        #endregion

}
}

完整的ASP.NET存储过程分页,排序,鼠标移至变色的更多相关文章

  1. asp.net存储过程分页+GridView控件 几百万数据 超快

    存储过程:---亲测275万数据,分页速度N快 ))+' '+@orderid+' from '+@tablename+' '+@tmpOrderid set @sql='select top'+st ...

  2. asp.net利用存储过程分页代码

    -最通用的分页存储过程 -- 获取指定页的数据 CREATE PROCEDURE Pagination ), -- 表名 ) = '*', -- 需要返回的列 )='', -- 排序的字段名 , -- ...

  3. ASP调用存储过程访问SQL Server

     ASP调用存储过程访问SQL Server 2011-02-15 10:22:57 标签:asp 数据库 sQL 存储过程 Server ASP和存储过程(Stored Procedures)的文章 ...

  4. SQL存储过程分页(通用的拼接SQL语句思路实现)

    多表通用的SQL存储过程分页 案例一: USE [Community] GO /****** Object: StoredProcedure [dbo].[Common_PageList] Scrip ...

  5. 存储过程分页 Ado.Net分页 EF分页 满足90%以上

    存储过程分页: create proc PR_PagerDataByTop @pageIndex int, @pageSize int, @count int out as select top(@p ...

  6. MS SQLSERVER通用存储过程分页

    最近在面试的时候,遇到个奇葩的秃顶老头面试官. 问:写过存储过程分页吗? 答:没写过,但是我知道分页存储的原理,我自己也写过,只是在工作中没写过. 问:那你这么多年工作中就没写过吗? 答:的确没写过, ...

  7. 【原创】10万条数据采用存储过程分页实现(Mvc+Dapper+存储过程)

    有时候大数据量进行查询操作的时候,查询速度很大强度上可以影响用户体验,因此自己简单写了一个demo,简单总结记录一下: 技术:Mvc4+Dapper+Dapper扩展+Sqlserver 目前主要实现 ...

  8. ligerUI---ligerGrid分页排序的使用(从后台获取数据显示)

    写在前面: 最近项目的前框框架用的是ligerUI,里面用到了ligerGrid表格,下面就来说说从后台获取数据并在前台页面进行完美展示.啊哈哈哈..(天啦,坐我旁边的丽姐貌似炒股 一个月可以搞几十万 ...

  9. 跟我学ASP.NET MVC之三:完整的ASP.NET MVC程序-PartyInvites

    摘要: 在这篇文章中,我将在一个例子中实际地展示MVC. 场景 假设一个朋友决定举办一个新年晚会,她邀请我创建一个用来邀请朋友参加晚会的WEB程序.她提出了四个注意的需求: 一个首页展示这个晚会 一个 ...

随机推荐

  1. c++ primer 9 顺序容器

    定义: #include <vector> #include <list> #include <deque> vector<int> svec; lis ...

  2. tornado中的cookie

    1. cookie与session的区别: Session:通过在服务器端记录用户信息从而来确认用户身份,保存在服务器上,每个用户会话都有一个对应的session Cookie:通过在客户端记录信息确 ...

  3. 15、Spark Streaming源码解读之No Receivers彻底思考

    在前几期文章里讲了带Receiver的Spark Streaming 应用的相关源码解读,但是现在开发Spark Streaming的应用越来越多的采用No Receivers(Direct Appr ...

  4. cocos2dx各个版本下载地址

    https://code.google.com/archive/p/cocos2d-x/downloads?page=1 各种工具包括 NDK 8 https://github.com/fusijie ...

  5. Python并发编程-守护进程

    守护进程 子进程转换为守护进程 主进程的代码结束,子进程的代码也应该接收, 这个事情有守护进程来做 守护进程会随着主进程的代码执行完毕而结束, 而不是随着主进程的接收而结束(子进程不一定结束) fro ...

  6. 「WC2016」论战捆竹竿

    「WC2016」论战捆竹竿 前置知识 参考资料:<论战捆竹竿解题报告-王鉴浩>,<字符串算法选讲-金策>. Border&Period 若前缀 \(pre(s,x)​\ ...

  7. 「BZOJ4763」雪辉

    「BZOJ4763」天野雪辉 题目大意:有一棵 \(n\) 个点的树,树上每一个点有权值 \(a_i \leq 30000\) ,每次询问给出若干路径,求出这些路径的并上面的不同颜色数与 \(mex\ ...

  8. [BZOJ4416][SHOI2013]阶乘字符串(子集DP)

    怎么也没想到是子集DP,想到了应该就没什么难度了. 首先n>21时必定为NO. g[i][j]表示位置i后的第一个字母j在哪个位置,n*21求出. f[S]表示S的所有全排列子序列出现的最后末尾 ...

  9. 【树链剖分/倍增模板】【洛谷】3398:仓鼠找sugar

    P3398 仓鼠找sugar 题目描述 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐厅(b),而 ...

  10. [bzoj1024][SCOI2009]生日快乐 (枚举)

    Description windy的生日到了,为了庆祝生日,他的朋友们帮他买了一 个边长分别为 X 和 Y 的矩形蛋糕.现在包括windy,一共有 N 个人来分这块大蛋糕,要求每个人必须获得相同面积的 ...