最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
gridview分页代码(可提高分页速度)
时间:2011-03-07 编辑:简简单单 来源:一聚教程网
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数据集。
-
上一个: asp.net js三级联动菜单实现方法
-
下一个: 对抽象编程:接口和抽象类
相关文章
- C#复制数组的两种方式及效率比较解读 10-24
- ASP.NET Identity用法解析 10-24
- ASP.NET MVC使用Identity增删改查用户介绍 10-24
- C语言中atoi函数模拟实现介绍 10-18
- .Net反向代理组件Yarp用法介绍 10-10
- .NET使用YARP通过编码方式配置域名转发实现反向代理教程 10-10