最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
gridview分页代码(可提高分页速度)
时间:2022-06-25 04:22:19 编辑:袖梨 来源:一聚教程网
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数据集。
相关文章
- 《崩坏星穹铁道》希露瓦光锥推荐介绍 04-20
- 《遗迹2》DLC2祈求者职业技能介绍 04-20
- 《崩坏星穹铁道》玲可阵容推荐介绍 04-20
- 《崩坏星穹铁道》黄泉突破材料一览 04-20
- 《崩坏星穹铁道》加拉赫抽取介绍 04-20
- 《崩坏星穹铁道》砂金突破材料一览 04-20