DAL层:
DalPageNav.cs:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using SinaSNS.Model;
using SinaSNS.Interface;
using SinaSNS.DBUtility;
namespace SinaSNS.Dal
{
/// <summary>
/// 数据访问类 DalPageNav
/// </summary>
public class DalPageNav : IPageNav
{
#region IPageNav 成员
public System.Data.DataTable OutDT(MPageNav mpn)
{
mpn.vodSetGetInputUrl();
mpn.vodSetGetPageNums();
if (mpn.intGetPageNum != 0)
{
if (mpn.intGetPageNum == 1)
{
mpn._PageMin = 0;
mpn._PageMax = mpn.intPagePerPage;
string strSql = "select e.* from (select ROW_NUMBER()over(order by " + mpn.strOrderBy + " " + mpn.strOrderByCode + ") as pagenum,f.* from (" + mpn.strIptSql + ") f ) e where pagenum >'" + mpn._PageMin + "' and pagenum <='" + mpn._PageMax + "'";
mpn.dt = SqlHelper.ExecuteDataset(SqlHelper.DBConn_SNS, CommandType.Text, strSql).Tables[0];
}
else
{
mpn._PageMin = (mpn.intGetPageNum - 1) * 10;
mpn._PageMax = (mpn.intGetPageNum - 1) * 10 + mpn.intPagePerPage;
string strSql = "select e.* from (select ROW_NUMBER()over(order by " + mpn.strOrderBy + " " + mpn.strOrderByCode + ") as pagenum,f.* from (" + mpn.strIptSql + ") f ) e where pagenum >'" + mpn._PageMin + "' and pagenum <='" + mpn._PageMax + "'";
mpn.dt = SqlHelper.ExecuteDataset(SqlHelper.DBConn_SNS, CommandType.Text, strSql).Tables[0];
}
}
this.GetSqlCount(mpn);
return mpn.dt;
}
/// <summary>
/// 设置页码
/// </summary>
private void GetSqlCount(MPageNav mpn)
{
string strSql = "select count(*) as total from (select ROW_NUMBER()over(order by " + mpn.strOrderBy + " " + mpn.strOrderByCode + ") as pagenum,f.* from (" + mpn.strIptSql + ") f ) e";
mpn._RowsCount = int.Parse(SqlHelper.ExecuteDataset(SqlHelper.DBConn_SNS, CommandType.Text, strSql).Tables[0].Rows[0][0].ToString());
if (mpn._RowsCount > 0 && mpn._RowsCount <= 9)//算出总页数
{
mpn._PageCount = 1;
}
else if (mpn._RowsCount % mpn._PageNums > 1 && mpn._RowsCount % mpn._PageNums < 9)
{
mpn._PageCount = (int)Math.Floor(mpn._RowsCount / (double)mpn._PageNums) + 1;
}
else if (mpn._RowsCount == 0)
{
mpn._PageCount = 1;
}
else
{
mpn._PageCount = (int)Math.Floor(mpn._RowsCount / (double)mpn._PageNums);
}
int j = mpn.intGetPageNum - 5;//输出页面的条件,并设置选中页为中间页
int intTemp = mpn.intGetPageNum + 5;
if (j <= 0)
{
j = 1;
intTemp = 11;
}
if (intTemp >= mpn._PageCount)
{
intTemp = mpn._PageCount + 1;
}
if (mpn._PageCount == 1)
{
j = 1;
intTemp = 2;
}
if (intTemp - j > mpn._PageNums)
intTemp -= 1;
if (mpn.strInputUrl != null)//判断Url输入类型
{
if (mpn.strInputUrl.IndexOf("?", 1, mpn.strInputUrl.Length - 1) == -1)
{
if (mpn.strInputUrl.IndexOf("?Page=", 1, mpn.strInputUrl.Length - 1) == -1)
{
mpn._UrlType = "?Page=";
}
else
{
mpn._UrlType = mpn._UrlType.Replace("?Page=", "");
}
}
else
{
if (mpn.strInputUrl.IndexOf("?Page=", 1, mpn.strInputUrl.Length - 1) == -1)
{
if (mpn.strInputUrl.IndexOf("&Page=", 1, mpn.strInputUrl.Length - 1) == -1)
{
mpn._UrlType = "&Page=";
}
else
{
int intTempQuery = mpn.strInputUrl.IndexOf("&Page=", 1, mpn.strInputUrl.Length - 2);
string strRemoveUrl = mpn.strInputUrl.Substring(intTempQuery);
mpn.strInputUrl = mpn.strInputUrl.Remove(intTempQuery, strRemoveUrl.Length);
mpn._UrlType = "&Page=";
}
}
else
{
mpn.strInputUrl = HttpContext.Current.Request.FilePath.ToString();
mpn._UrlType = "?Page=";
}
}
}
else
{
mpn._UrlType = "&Page=";
}
while (j < intTemp)//输出页码
{
// if(j==mpn_PageCount)
// break;
if (mpn.intGetPageNum == j)//当前页条件
{
mpn._PageNumsOut += mpn._PageButton.Replace("$", "<font color='red'>" + j + "</font>").Replace("#", mpn.strInputUrl + mpn._UrlType + j).Replace("css", mpn.strButtonCss);
}
else
{
mpn._PageNumsOut += mpn._PageButton.Replace("$", j.ToString()).Replace("#", mpn.strInputUrl + mpn._UrlType + j).Replace("css", mpn.strButtonCss);
}
j++;
}
if (mpn.intGetPageNum == mpn._PageCount)//下一页条件
{
mpn._PageNext = "";
mpn._ImgNext = "";
}
else if (mpn.intGetPageNum == 1)
{
mpn._PageNext = mpn._PageNext.Replace("#", mpn.strInputUrl + mpn._UrlType + Convert.ToString(mpn.intGetPageNum + 1)).Replace("text", mpn.strNextLabel).Replace("css", mpn.strNextCss);
mpn._ImgNext = mpn._ImgNext.Replace("sc", mpn.strImgNextSrc).Replace("wd", mpn.intImgWidth.ToString()).Replace("ht", mpn.intImgHeight.ToString());
}
else
{
mpn._PageNext = mpn._PageNext.Replace("#", mpn.strInputUrl + mpn._UrlType + Convert.ToString(mpn.intGetPageNum + 1)).Replace("text", mpn.strNextLabel).Replace("css", mpn.strNextCss);
}
if (mpn.intGetPageNum - 1 <= 0 || mpn.intGetPageNum == 1)//上一页条件
{
mpn._PagePre = "";
mpn._ImgPre = "";
}
else
{
mpn._PagePre = mpn._PagePre.Replace("#", mpn.strInputUrl + mpn._UrlType + Convert.ToString(mpn.intGetPageNum - 1)).Replace("text", mpn.strPreLabel).Replace("css", mpn.strPreCss);
mpn._ImgPre = mpn._ImgPre.Replace("sc", mpn.strImgPreSrc).Replace("wd", mpn.intImgWidth.ToString()).Replace("ht", mpn.intImgHeight.ToString());
}
if (mpn.intGetPageNum == mpn._PageCount)//设置最后页,第一页
{
mpn._PageLast = "";
mpn._PageFirst = mpn._PageFirst.Replace("#", mpn.strInputUrl + mpn._UrlType + 1).Replace("text", mpn.strFirstLabel).Replace("css", mpn.strFirstCss);
}
else if (mpn.intGetPageNum == 1)
{
mpn._PageFirst = "";
mpn._PageLast = mpn._PageLast.Replace("#", mpn.strInputUrl + mpn._UrlType + mpn._PageCount).Replace("text", mpn.strLastLabel).Replace("css", mpn.strLastCss);
}
else
{
mpn._PageLast = mpn._PageLast.Replace("#", mpn.strInputUrl + mpn._UrlType + mpn._PageCount).Replace("text", mpn.strLastLabel).Replace("css", mpn.strLastCss);
mpn._PageFirst = mpn._PageFirst.Replace("#", mpn.strInputUrl + mpn._UrlType + 1).Replace("text", mpn.strFirstLabel).Replace("css", mpn.strFirstCss);
}
}
#endregion
}
}