Asp.net GridView分页功能的实现

	最近学习了Asp.net的GridView分页的实现,当然,GridView自带分页功能,但是这个分页功能的后台数据库操作的时候仍是
需要查询出所有的记录,只是前台页面显示GridView的时候有一种分页的感觉,但这其实是假的,是一种前台的效果而已,
对后台数据库的操作是没有优化的,当记录很少的时候,性能没有影响,当记录变多的时候,性能就会逐步的降低。
		所以我们需要采用另一个组件,把AspNetPager.dll和AspNetPager引用到C#组件里面去才可以使用
			1.首先把上面这两个文件复制到项目名字为"bin"的文件夹里面。
			2.在工具箱引用这个组件,首先需要右键点击工具箱空白页面,"添加选项卡",名字可以自定义,添加完成过后再右键点
			击自己所创建的选项卡,选择"选择项",再.Net Framework组件中点击浏览把刚才的"AspNetPager.dll"添加进去,然后
			打钩,点击确定。![在这里插入图片描述](https://img-blog.csdnimg.cn/20190612120143534.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDQzNTYxMg==,size_16,color_FFFFFF,t_70)
			在添加完成过后就可以看到AspNetPager这个组件了![在这里插入图片描述](https://img-blog.csdnimg.cn/20190612120330697.png)
			
			数据库实现分页的SQL语句
declare @currentPage int			//声明整型变量currentPage
set @currentPage=3					//赋值语句
select * from
(select ROW_NUMBER() over(order by xh) rowid,xh,xm,csrq,ssh from tblStudentInfo)a		//ROW_NUMBER:通过字段xh进行由小到大的排序
where rowid>=(@currentPage-1)*10+1 and rowid<=@currentPage*10			//第一页1-10,第二页11-20,第三页21-30


DBcon类的代码

public class DBcon
{
//定义连接数据库的字符串
string constr = ConfigurationManager.ConnectionStrings[“constr”].ToString();

    //对数据库的查询
    public DataSet Query(string strSql)
    {
        SqlConnection con = new SqlConnection(constr); //连接数据库
        //由于是查询,所以这里不需要打开数据库
        SqlDataAdapter da = new SqlDataAdapter(strSql,con);  //数据适配器
        DataSet ds = new DataSet(); //实例化一个数据集
        da.Fill(ds);    //把数据适配器查询的内容填充到数据集ds里面
        return ds;
    }


    //对数据库的增、删、改
    public int ExeSql(string strSql)
    {
        SqlConnection con = new SqlConnection(constr);
        con.Open();     //由于是对数据库的增删改,所以我们需要打开数据库
        SqlCommand cmd = new SqlCommand(strSql,con);    //实例化命令
        int rows;       //因为是返回影响的行数,所以声明一个整型变量接收所影响的行数
        rows = cmd.ExecuteNonQuery();
        con.Close();
        return rows;
    }



    //接收数据库第一行第一列的记录
    public object GetSingle(string strSql)
    {
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        SqlCommand cmd = new SqlCommand(strSql,con);
        object obj = cmd.ExecuteScalar();
        return obj;
    }
}

前台页面视图

在这里插入图片描述

代码实现

 public partial class WebForm1 : System.Web.UI.Page
    {
        DBcon db = new DBcon();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AspNetPager1.RecordCount = int.Parse(db.GetSingle("select COUNT(*) from tblStudentInfo").ToString());//记录数据库的记录数量
                AspNetPager1.PageSize = 30;     //每页显示的记录的数量
                DataGridBind(1, AspNetPager1.PageSize);      	//第一个参数为第一页的记录,第二个参数为第一页显示记录的数量
  
            }
        }
        public void DataGridBind(int CurrentPage, int PageSize)   //CurrentPage:当前页;PageSize:当前页面显示的记录数量;
        {
            string strSql = string.Format("select * from" +
" (select ROW_NUMBER() over(order by xh) rowid,xh,xm,csrq,ssh from tblStudentInfo)a" +
" where rowid>=({0}-1)*{1}+1 and rowid<={0}*{1}", CurrentPage, PageSize);
            GridView1.DataSource = db.Query(strSql);
            GridView1.DataBind();
        }

        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            DataGridBind(e.NewPageIndex,AspNetPager1.PageSize);    //第一个参数是获取当前页面的索引,第二个参数是获取每页显示记录的数量

        }
    }
	以上就是GridView的实现,SQL语句无论是在C#还是PHP都可以使用的!初学者,有错误请反馈!
发布了2 篇原创文章 · 获赞 0 · 访问量 196

猜你喜欢

转载自blog.csdn.net/weixin_44435612/article/details/91502140