日期:2014-05-17 浏览次数:20443 次
string accessConnection = ConfigurationManager.ConnectionStrings["accessConnection"].ConnectionString;
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.AspNetPager1.PageSize = 10; this.AspNetPager1.RecordCount = Convert.ToInt32(OpenCode.AccessHelper.GetExecuteScalar("select count(*) from news as a inner join newstype as b on(a.ntypeid=b.ntid) where a.nlanguage=0 ", null)); GridDataBinds(1); } } private void GridDataBinds(int pageIndex) { int pageSize = this.AspNetPager1.PageSize; string strSql = PageCode.GetCommonPaging(" select top 10 nid,ntitle,ncontents,ndate,ntname,nrecommend,nsequence from news as a inner join newstype as b on(a.ntypeid=b.ntid)", pageSize, pageIndex, "nid", " and a.nlanguage=0 ", "from news as a inner join newstype as b on(a.ntypeid=b.ntid) "); this.GridView1.DataSource = OpenCode.AccessHelper.GetReader(strSql, null); this.GridView1.DataKeyField = "nid"; this.GridView1.DataBind(); } protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e) { GridDataBinds(e.NewPageIndex); } 封转方法 /// <summary> /// 拼凑分页语句 /// </summary> /// <param name="strSql">sql语句</param> /// <param name="strtab">表名</param> /// <param name="pageSize">分页大小</param> /// <param name="pageIndex">当前页</param> /// <param name="orderfield">排序字段</param> /// <returns></returns> public static string GetCommonPaging(string strSql, string strtab, int pageSize, int pageIndex, string orderfield,string strWhere) { string str = ""; if (pageIndex > 1) { str = string.Format("{0} where 1=1 {4} and {1}>( select max({1}) " + " from( select top {2} {1} from {3} where 1=1 {4} order by {1}) as {1} ) order by {1} ", strSql, orderfield, (pageSize * (pageIndex - 1)).ToString(), strtab, strWhere); } else { str = string.Format("{0} order by {1}", strSql, orderfield); } return str; } public static string GetCommonPaging(string strSql, int pageSize, int pageIndex, string orderfield, string strWhere,string strchild) { string str = ""; if (pageIndex > 1) { str = string.Format("{0} where 1=1 {3} and {1}>( select max({1}) " + " from( select top {2} {1} {4} where 1=1 {3} order by {1}) as {1} ) order by {1} ", strSql, orderfield, (pageSize * (pageIndex - 1)).ToString(), strWhere, strchild); } else { str = string.Format("{0} where 1=1 {2} order by {1}", strSql, orderfield, strWhere); } return str; } PS 分页语句原型: [code=SQL] SELECT TOP 页大小 * FROM table1 WHERE id > ( SELECT ISNULL(MAX(id),0) FROM ( SELECT TOP 页大小*(页数-1) id FROM table1 ORDER BY id ) A ) ORDER BY id