gridview分页代码(可提高分页速度)

作者:袖梨 2022-06-25

gridview分页代码(可提高分页速度)

internal static datatable datareadertodatatable(string squery, int istart, int iend)
        {
datatable schematable = null;
            datatable dt = null;
            sqlcommand cmdsql;
            sqldatareader dr = null;
            sqlconnection conn = null;
            long icount = 0;
            try
            {
                //打开数据库教程连接,执行datareader         
                conn = new sqlconnection(connstring);
                conn.open();
                cmdsql = new sqlcommand(squery, conn);
                dr = cmdsql.executereader(commandbehavior.closeconnection);
                schematable = dr.getschematable();
                dt = new datatable();

                //get the schema of tables columns and its types, and load the same into datatable.
                for (int i = 0; i <= schematable.rows.count - 1; i++)
                {
                    datarow drow = schematable.rows[i];
                    datacolumn column = new datacolumn();
                    column.datatype = system.type.gettype(drow["datatype"].tostring());
                    column.allowdbnull = (drow["allowdbnull"].tostring() == "true" ? true : false);
                    column.columnname = drow["columnname"].tostring();
                    column.caption = drow["columnname"].tostring();
                   
                    dt.columns.add(column);
                    //more datatable property can be added as required.
                }
                if (istart == 0) istart = 1;
                if (iend == 0) iend = 1;
                icount = 1;

                //loop the reader which is executed till the start and variable,
                //fetch and add the rows one by one to data table till the end count is reached.
                // exit the loop and return datable.
                while (dr.read())
                {
                    if (icount >= istart && icount <= iend)
                    {
                        datarow drow = dt.newrow();
                        for (int i = 0; i <= dr.fieldcount - 1; i++)
                        {
                            drow[i] = dr.getvalue(i);
                        }
                        dt.rows.add(drow);
                    }
                    else if (icount > iend)
                    {
                        break;
                    }
                    icount = icount + 1;
                }
            }
            catch (systemexception ex)
            {
                throw ex;
            }
            finally
            {
                conn.close();
                conn.dispose();
                schematable.dispose();
                dr.close();
                dr.dispose();
            }
            return dt;
        }


在后台绑定到gridview的方法如下:

private void binddata(int pageindex)
        {
int startrow;
            int endrow;

            startrow = (pageindex * grdemployee.pagesize) +1;
            endrow= startrow + grdemployee.pagesize -1;
           

        grdemployee.datasource= custompaging.class.common.datareadertodatatable(selectquery,startrow,endrow);
            grdemployee.databind();
          
        } 

1、执行datareader

2、从我们规定的开始的位置执行datareader

3、执行并得到数据记录

4、将数据记录放到datatable数据集中

5、结束datareader

6、返回datatable数据集。

 

相关文章

精彩推荐