asp中AspNetPager分页控件的sql和存储过程的用法

基于班上的需求简单的使用AspNetPager分页控件的两种方法

第一步首先去网上下载AspNetPager插件

第二步将控件放到项目的bin问件下
asp中AspNetPager分页控件的sql和存储过程的用法
将控件导入到项目中去:事项右击工具箱中的选择项
asp中AspNetPager分页控件的sql和存储过程的用法

点击浏览将bin文件夹下的AspNetPager导入进去asp中AspNetPager分页控件的sql和存储过程的用法
有时会出现后台点不出包可以在项目中添加引用后点击浏览再次将AspNetPager导入asp中AspNetPager分页控件的sql和存储过程的用法

将控件拖放到页面后我们需要知道AspNetPager插件的三个属性
RecordCount是数据的总行数,它要知道你查的数据总共有多少条
PageSize是页面显示行数,是一页中显示多少条数据
CurrentPageIndex是当前页面的索引,指页码

三层中需要DAL层中的代码

//查询总行数得到RecordCount数据的总行数
public static int selectcountStu()
{
string sql = string.Format(" select count(*) from stuInfo ");
DataTable dt = DBHelper.GetDataTable(sql);
if (dt!=null)
{
return int.Parse(dt.Rows[0][0].ToString());
}
else
{
return 0;
}
}

///
/// 查询学生信息 Top就是PageSize指一页显示的行数,
///假如你查询的是第三页的数据,那你就得除去前两页的数据,所以({1}-1){0})就是当前页面索引减去1再乘以一页显示的数据得到的是前面两页的数据 not in 是指不在这些数据中,所以这条语句的意思就是除去前两页的数据查询第三页的数据
///

///
public static List selectAllDAl(int PageSize,int currtIndex)
{
string sql = string.Format( @" select top {0} *
from stuInfo s,classInfo c,DegreeInfo d where s.classId=c.classId and s.DegreeID=d.DegreeID and stuID
not in (select top (({1}-1){0}) stuID from stuInfo )order by s.stuID", PageSize, currtIndex);

        DataTable dt = DBHelper.GetDataTable(sql);
        List<StuInfoModel> list = new List<StuInfoModel>();
        if (dt.Rows.Count > 0)
        {
            foreach (DataRow dr in dt.Rows)
            {
                StuInfoModel stu = new StuInfoModel();
                stu.StuID = dr["stuID"].ToString();
            
                stu.StuName = dr["stuName"].ToString();
                stu.StuGender = dr["stuGender"].ToString();
               
               
                stu.StuImg = dr["stuImg"].ToString();
                stu.StuBirthday = DateTime.Parse(dr["stuBirthday"].ToString());
            
                stu.DegModel.DegreeName = dr["DegreeName"].ToString();
                stu.ClassModel.ClassName = dr["className"].ToString();
                list.Add(stu);
            }
            return list;
        }
        else
        {
            return null;
        }
    }

Bll层就是直接调用

//查询总行数
public static int selectcountStu()
{
return StuClassDAL.selectcountStu();
}
///
/// 查询学生信息
///
/// true/false
public static List selectAllBLL(int pageindex, int currtIndex)
{
return StuClassDAL.selectAllDAl(pageindex,currtIndex);
}

UI层

public void bind()
{
//获取到RecordCount 中的数据的总行数
this.AspNetPager1.RecordCount = StuAllBLL.selectcountStu();
//传参数一页PageSize显示的数据行数和CurrentPageIndex当前页面的索引
this.Repeater1.DataSource = StuAllBLL.selectAllBLL(this.AspNetPager1.PageSize, this.AspNetPager1.CurrentPageIndex);
this.Repeater1.DataBind();
}
在AspNetPager的PageChanged事件中调用函数
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
bind();
}

存储过程的用法

打开AspNetPager1任务
点击存储过程生成工具

asp中AspNetPager分页控件的sql和存储过程的用法
asp中AspNetPager分页控件的sql和存储过程的用法
注册它会自己给你三个变量
@startIndex int,//开始
@endIndex int,//结束
@docount bit
存储过程的查询startIndex 是开始到endIndex 结束
也就是查询第多少条到多少条之间的数据
如果想加其他的变量的话点击注册

点击生成后是这样的
create procedure proc_select
(@startIndex int,
@endIndex int,
@docount bit)
as

if(@docount=1)
select count(*) from stuInfo s,classInfo c,DegreeInfo d where s.classid=c.classid and s.degreeid=d.degreeid
else
begin
with temptbl as (
SELECT ROW_NUMBER() OVER (ORDER BY stuID desc)AS Row, * from stuInfo s,classInfo c,DegreeInfo d where s.classid=c.classid and s.degreeid=d.degreeid)
SELECT * FROM temptbl where Row between @startIndex and @endIndex
end

因为@docoun在后面没有用到所以我将其删掉了,根据我的需求改成了
这里说一下如果你查询多表的话两个表有着相同的字段的话temptbl 会提示你为它指定了多个相同的字段这个时候你就需要将你要查的子段放到Rows后面,过滤掉相同的字段

create procedure proc_select
(@startIndex int,
@endIndex int
)
as
begin
with temptbl as (
SELECT ROW_NUMBER() OVER (ORDER BY stuID desc)AS Row,s.stuID,s.stuQQ,s.stuPhone,s.stuAddress,s.stuHobby, s.stuName,s.stuGender,s.stuBirthday,c.className,d.DegreeName,s.stuImg,c.classId,d.DegreeID
from stuInfo s,classInfo c,DegreeInfo d where s.classid=c.classid and s.degreeid=d.degreeid)
SELECT * FROM temptbl where Row between @startIndex and @endIndex
end

同时DBHelper中

需要有查询存储过程的方法
///
/// 获取DataSet数据集(通过存储过程)
///
/// CommandType类型
/// 存储过程名
/// 参数
public static DataSet GetDataSetByProc(CommandType type, string procName, params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = procName;
cmd.CommandType = type;
cmd.Parameters.AddRange(parameters);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
using (DataSet ds = new DataSet())
{
try { da.Fill(ds); return ds; }
catch (SqlException)
{
return null;
}
finally
{
conn.Close();
}

                    }
                }
            }
        }
    }

**

DAL层中

**
也是一个查询总行数,只是把后面的查询改成了存储过程
存储过程的查询pagesize是开始到endpage结束
也就是查询第多少条到多少条之间的数据
//查询总行数
public static int selectStuCount()
{
string sql = string.Format(“select count(*) from stuInfo”);
DataTable dt = DBHelper.GetDataTable(sql);
if (dt!=null)
{
return int.Parse(dt.Rows[0][0].ToString());
}
else
{
return 0;
}
}
///
/// 查询学生信息
///
///
///
public static List selectAllDAl(int pagesize,int endpage)
{
SqlParameter[] paremeters =
{
new SqlParameter("@startIndex",pagesize),
new SqlParameter("@endIndex",endpage)
};
//proc_select是存储过程的名字
DataSet ds = DBHelper.GetDataSetByProc(CommandType.StoredProcedure, “proc_select”,paremeters);
List list = new List();
DataTable dt = ds.Tables[0];
if (dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
StuInfoModel stu = new StuInfoModel();
stu.StuID = dr[“stuID”].ToString();
stu.ClassId = dr[“classId”].ToString();
stu.StuName = dr[“stuName”].ToString();
stu.StuGender = dr[“stuGender”].ToString();
stu.StuHobby = dr[“stuHobby”].ToString();
stu.StuAddress = dr[“stuAddress”].ToString();
stu.StuPhone = dr[“stuPhone”].ToString();
stu.StuImg = dr[“stuImg”].ToString();
stu.StuBirthday = DateTime.Parse(dr[“stuBirthday”].ToString());
stu.StuQQ = dr[“stuQQ”].ToString();
stu.DegreeID = int.Parse(dr[“DegreeID”].ToString());
stu.DegModel.DegreeName = dr[“DegreeName”].ToString();
stu.ClassModel.ClassName = dr[“className”].ToString();
list.Add(stu);
}
return list;
}
else
{
return null;
}
}

BLL层

//查询总行数
public static int selectStuCount()
{
return StuClassDAL.selectStuCount();
}
///
/// 查询学生信
///
/// true/false
public static List selectAllBLL(int pagesize, int endpage)
{
return StuClassDAL.selectAllDAl(pagesize,endpage);
}

UI层

 //分页数据显示绑定
    public void Bind()
    {
        this.AspNetPager1.RecordCount = StuAllBLL.selectStuCount();//查询总数据数行数
        this.Repeater1.DataSource = StuAllBLL.selectAllBLL(AspNetPager1.PageSize*(AspNetPager1.CurrentPageIndex-1)+1,AspNetPager1.CurrentPageIndex*AspNetPager1.PageSize);
        //计算从第多少条开始到多少条结束
        this.Repeater1.DataBind();
    }
    //分页点击事件调用函数
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        Bind();
    }