Sql分页存储过程以及实现

创建一个类文件,内容如下: using System; using DataHelper; using System.Data; using System.Data.SqlClient; using System.Web; using System.Configuration; namespace ClassBase { public class Paging { #region 初始值 private int pagesize=10;//每页显示的记录数 private int pageindex=1;//当前要显示的页号 private string pkname="ID";//主键名 private string table;//不包含from关键子,如:myTable或者myTable,yourTable private string fields="*";//不包含select关键字,如:*或者Id,UserId,UserName等 private string orderby="ID ASC";//order by 子句,不包含order by 子句 ,如id desc,UserId asc 等 private string where="";//Where子句,不包含where关键字,如空的,或者 id>2 等 private int ordertype=1; private string link; #endregion #region 初始值及属性 public string PkName { get{return this.pkname;} set{this.pkname = value;} } /// <summary> /// 表名 /// </summary> public string Table { get{return this.table;} set{this.table = value;} } /// <summary> /// 需要返回的列 /// </summary> public string Fields { get{return this.fields;} set{this.fields = value;} } /// <summary> /// 排序的字段名 /// </summary> public string OrderBy { get{return this.orderby;} set{this.orderby = value;} } /// <summary> /// 条件 /// </summary> public string Where { get{return this.where;} set{this.where = value;} } /// <summary> /// 每页显示的条数 /// </summary> public int PageSize { get{return this.pagesize;} set{ if(this.pagesize > 1) this.pagesize = value; else this.pagesize=Convert.ToInt32(ConfigurationSettings.AppSettings["PageSize"]); } } /// <summary> /// 当前第几页 /// </summary> public int PageIndex { get{return this.pageindex;} set{ if(this.pageindex > 0) this.pageindex = value; else this.pageindex = 1; } } /// <summary> /// 0为顺序,1为倒序 /// </summary> public int OrderType { get{return this.ordertype;} set{ this.ordertype=value;} } public string Link { get{return this.link;} } #endregion public Paging() { if(ordertype==1) this.orderby="ID Desc"; } public Paging(string table,string fields,string orderby,string pkname,int pagesize,int pageindex,int ordertype,string where) { this.table = table; this.fields = fields; //this.orderby = orderby; this.pkname = pkname; this.pagesize = pagesize; this.pageindex = pageindex; this.ordertype = ordertype; this.where = where; if(ordertype==1) this.orderby="ID Desc"; } /// <summary> /// 返回分页后的记录 /// </summary> /// <param name="url"></param> /// <returns></returns> public DataTable GetList(string url) { int sumcount;//总记录数 int sumpage;//总页数 string sql="PageShowOne";//存储过程名 /*@PageSize int=10 ,--每页显示的记录数 ///@PageCurrent int=1 ,--当前要显示的页号 ///@FdName varchar(100)='' ,--主键名或者标识列名 ///@SelectStr varchar(2000)='', --select子句,不包含select关键字,如:*或者Id,UserId,UserName等。 ///@FromStr varchar(1000)='', --from子句,不包含from关键子,如:myTable或者myTable,yourTable ///@WhereStr varchar(2000)='', --Where子句,不包含where关键字,如空的,或者 id>2 等 ///@OrderByStr varchar(1000)='',--order by 子句,不包含order by 子句 ,如id desc,UserId asc 等 ///@CountRows int=0 output, --返回记录总数 ///@CountPage int=0 output --返回总页数 */ SqlParameter[] para=new SqlParameter[]{       new SqlParameter("@PageSize",this.pagesize),       new SqlParameter("@PageCurrent",this.pageindex),       new SqlParameter("@FdName",this.pkname),       new SqlParameter("@SelectStr",this.fields),       new SqlParameter("@FromStr",this.table),       new SqlParameter("@WhereStr",this.where),       new SqlParameter("@OrderByStr",this.orderby),       new SqlParameter("@CountRows",SqlDbType.Int),       new SqlParameter("@CountPage",SqlDbType.Int)                 }; para[7].Direction = ParameterDirection.Output; DataTable dt=new DataTable(); dt=DataHelper.SqlHelper.ExecuteDataset(GetCon.ConString,CommandType.StoredProcedure,sql,para).Tables[0]; sumcount=Convert.ToInt32(para[7].Value);//取得总记录数 sumpage=(int)Math.Ceiling((double)sumcount/pagesize);//计算分成多少页 int iStart=pageindex-3; int iEnd=pageindex+2; if(iStart<0) iStart = 0; if(iEnd>sumpage) iEnd =sumpage; if(sumpage>1) { string gourl="<input type=/"text/" id=/"txturl/" value=/""+this.pageindex+"/" style=/"width:30px;/" /><input type=/"button/" onclick=/"gotourl();/" value=/" GO /" />"; string fpage="<a href=/""+url+"pageindex=1/" {0}><font face=/"webdings/" title='转到第一页'>9</font></a> ";//第一页 string npage="<a href=/""+url+"pageindex="+Convert.ToString(this.pageindex + 1)+"/" {0} title='转到下一页'><font face=/"webdings/">4</font></a> ";//下一页 string ppage="<a href=/""+url+"pageindex="+Convert.ToString(this.pageindex - 1)+"/" {0} title='转到上一页'><font face=/"webdings/">3</font></a> ";//上一页 string lpage="<a href=/""+url+"pageindex="+Convert.ToString(sumpage)+"/" {0} title='转到最后一页'><font face=/"webdings/">:</font></a> ";//最后一页 link = "共<b>"+ sumcount.ToString() + "</b>条记录,分成<b>" + sumpage.ToString() + "</b>页,当前第<b>"+pageindex.ToString()+"</b>页 "; if(this.pageindex == 1) { //link += string.Format(fpage,"disabled=/"true/"");//第一页 //link +=string.Format(ppage,"disabled=/"true/"");// + gourl; //link += for(int i=iStart;i<iEnd;i++) {   if((i+1)==pageindex)   link +="<u>"+Convert.ToString(i+1)+"</u> ";   else   link +="<a href=/""+url+"pageindex="+Convert.ToString(i+1)+"/" title=/"跳转到第"+Convert.ToString(i+1)+"页/"><b>"+Convert.ToString(i+1)+"</b></a> "; } link+=string.Format(npage,""); link+=string.Format(lpage,""); link +=gourl; } else if(this.pageindex == sumpage) { link += string.Format(fpage,"");//第一页 link +=string.Format(ppage,"");// + gourl; //link += for(int i=iStart;i<iEnd;i++) {   if((i+1)==pageindex)   link +="<u>"+Convert.ToString(i+1)+"</u> ";   else   link +="<a href=/""+url+"pageindex="+Convert.ToString(i+1)+"/" title=/"跳转到第"+Convert.ToString(i+1)+"页/"><b>"+Convert.ToString(i+1)+"</b></a> "; } //link+=string.Format(npage,""); //link+=string.Format(lpage,""); link +=gourl; } else { link += string.Format(fpage,"");//第一页 link +=string.Format(ppage,"");// + gourl; //link += for(int i=iStart;i<iEnd;i++) {   if((i+1)==pageindex)   link +="<u>"+Convert.ToString(i+1)+"</u> ";   else   link +="<a href=/""+url+"pageindex="+Convert.ToString(i+1)+"/" title=/"跳转到第"+Convert.ToString(i+1)+"页/"><b>"+Convert.ToString(i+1)+"</b></a> "; } link+=string.Format(npage,""); link+=string.Format(lpage,""); link +=gourl; } link [url=mailto:+=@]function">+=@"<script>function gotourl(){var pageindex=document.getElementById('txturl').value;"; link [url=mailto:+=@]+=@"var pattern=/^/d+$/;"; link [url=mailto:+=@]+=@"if(!pattern.test(pageindex) || pageindex.length==0 || pageindex=='' || pageindex<=0){alert('请输入一个大与0的整数!');return false;}"; link [url=mailto:+=@]"+sumpage+"){alert('">+=@"if(pageindex>"+sumpage+"){alert('索引超出范围!');return false;}"; link [url=mailto:+=@]+=@"[window.location.href]='"+url+"pageindex='+pageindex;}</script>"; } return dt; } } } 创建一个wen窗体,该窗体拥有一控件Repeater ,一个 Label,在 .cs页面添加一个方法,方法如下: private void binddata() {   Paging p = new Paging();   p.Table = "VIEW_DressProvide";   p.Fields = "*";   p.PageIndex = pageindex;   p.PageSize = 7;   p.OrderBy = "id desc";   p.PkName = "";   p.Where = "";   DataTable dt = p.GetList("?");   if (dt.Rows.Count > 0)   {     this.Repeater1.Visible = true;     this.Label1.Text = "<div align='center'>" + p.Link + "</div>";     this.Repeater1.DataSource = dt;     this.Repeater1.DataBind();   }   else   {     this.Repeater1.Visible = false;     this.Label1.Text = "<table width='756' border='1' bordercolor='#CCCCCC' bordercolordark='#FFFFFF' cellpadding='0' cellspacing='0'><tr><td height='50' align='center'>对不起!本页没有记录!</td></tr></table>";   }   dt.Dispose(); } 在Page_Load进行调用 if(!Page.IsPostBack) { this.binddata(); } 到此分页成功

你可能感兴趣的:(sql,String,table,url,存储,output)