当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 大数量查询分页显示 微软的解决办法

ASP.NET
.Net业务平台的数值精度陷阱与解决方法
投票结果的图片进度条显示
七招制胜ASP.NET应用程序开发
ASP.NET 2.0的导航控件treeview和menu的实例
ASP.NET实现页面间值传递的几种方法
项目调试时出现用到的一个组件“访问被拒绝”的解决方法
ASP.NET中“找不到指定模块”的解决办法
C#中的委托和事件
在指定应用程序域中执行代码
Ajax简单客户登陆验证
AJAX将成为移动Web2.0时代首选开发平台
关闭按钮点击事件的捕捉
远程抓取GOOGLE的自动翻译结果
保护你的DLL和Code不被别人使用
ASP.NET的底层的工作机制介绍
ASP.NET2.0服务器控件之捕获回传事件
使用Data Access Application Block 得到存储过程的返回值
面向服务及其在互联系统策略中的角
ASP.NET2.0服务器控件之创建复杂属性
ASP.NET:设置页面buffer引出来的问题

ASP.NET 中的 大数量查询分页显示 微软的解决办法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 70 ::
收藏到网摘: n/a

微软的解决办法
usingSystem;
usingSystem.Data;
usingSystem.Data.SqlClient;
usingSystem.Drawing;
usingSystem.Windows.Forms;

publicclassPagingSample:Form
{
//Formcontrols.
ButtonprevBtn=newButton();
ButtonnextBtn=newButton();

staticDataGridmyGrid=newDataGrid();
staticLabelpageLbl=newLabel();

//Pagingvariables.
staticintpageSize=10;//Sizeofviewedpage.
staticinttotalPages=0;//Totalpages.
staticintcurrentPage=0;//Currentpage.
staticstringfirstVisibleCustomer="";//Firstcustomeronpagetodeterminelocationformoveprevious.
staticstringlastVisibleCustomer="";//Lastcustomeronpagetodeterminelocationformovenext.

//DataSettobindtoDataGrid.
staticDataTablecustTable;

//InitializeconnectiontodatabaseandDataAdapter.
staticSqlConnectionnwindConn=newSqlConnection("DataSource=localhost;IntegratedSecurity=SSPI;InitialCatalog=northwind");
staticSqlDataAdaptercustDA=newSqlDataAdapter("",nwindConn);
staticSqlCommandselCmd=custDA.SelectCommand;

publicstaticvoidGetData(stringdirection)
{
//CreateSQLstatementtoreturnapageofrecords.
selCmd.Parameters.Clear();

switch(direction)
{
case"Next":
selCmd.CommandText="SELECTTOP"+pageSize+"CustomerID,CompanyNameFROMCustomers"+
"WHERECustomerID>@CustomerIdORDERBYCustomerID";
selCmd.Parameters.Add("@CustomerId",SqlDbType.VarChar,5).Value=lastVisibleCustomer;
break;
case"Previous":
selCmd.CommandText="SELECTTOP"+pageSize+"CustomerID,CompanyNameFROMCustomers"+
"WHERECustomerID<@CustomerIdORDERBYCustomerIDDESC";
selCmd.Parameters.Add("@CustomerId",SqlDbType.VarChar,5).Value=firstVisibleCustomer;
break;
default:
selCmd.CommandText="SELECTTOP"+pageSize+"CustomerID,CompanyNameFROMCustomersORDERBYCustomerID";

//Determinetotalpages.
SqlCommandtotCMD=newSqlCommand("SELECTCount(*)FROMCustomers",nwindConn);
nwindConn.Open();
inttotalRecords=(int)totCMD.ExecuteScalar();
nwindConn.Close();
totalPages=(int)Math.Ceiling((double)totalRecords/pageSize);

break;
}

//Fillatemporarytablewithqueryresults.
DataTabletmpTable=newDataTable("Customers");
intrecordsAffected=custDA.Fill(tmpTable);

//Iftabledoesnotexist,createit.
if(custTable==null)
custTable=tmpTable.Clone();

//Refreshtableifatleastonerecordreturned.
if(recordsAffected>0)
{
switch(direction)
{
case"Next":
currentPage++;
break;
case"Previous":
currentPage--;
break;
default:
currentPage=1;
break;
}

pageLbl.Text="Page"+currentPage+"of"+totalPages;

//Clearrowsandaddnewresults.
custTable.Rows.Clear();

foreach(DataRowmyRowintmpTable.Rows)
custTable.ImportRow(myRow);

//Preservefirstandlastprimarykeyvalues.
DataRow[]ordRows=custTable.Select("","CustomerIDASC");
firstVisibleCustomer=ordRows[0][0].ToString();
lastVisibleCustomer=ordRows[custTable.Rows.Count-1][0].ToString();
}
}

publicPagingSample()
{
//Initializecontrolsandaddtoform.
this.ClientSize=newSize(360,274);
this.Text="NorthWindData";

myGrid.Location=newPoint(10,10);
myGrid.Size=newSize(340,220);
myGrid.AllowSorting=true;
myGrid.CaptionText="NorthWindCustomers";
myGrid.ReadOnly=true;
myGrid.AllowNavigation=false;
myGrid.PreferredColumnWidth=150;

prevBtn.Text="<<";
prevBtn.Size=newSize(48,24);
prevBtn.Location=newPoint(92,240);
prevBtn.Click+=newEventHandler(Prev_OnClick);

nextBtn.Text=">>";
nextBtn.Size=newSize(48,24);
nextBtn.Location=newPoint(160,240);

pageLbl.Text="NoRecordsReturned.";
pageLbl.Size=newSize(130,16);
pageLbl.Location=newPoint(218,244);

this.Controls.Add(myGrid);
this.Controls.Add(prevBtn);
this.Controls.Add(nextBtn);
this.Controls.Add(pageLbl);
nextBtn.Click+=newEventHandler(Next_OnClick);


//PopulateDataSetwithfirstpageofrecordsandbindtogrid.
GetData("Default");
DataViewcustDV=newDataView(custTable,"","CustomerID",DataViewRowState.CurrentRows);
myGrid.SetDataBinding(custDV,"");
}

publicstaticvoidPrev_OnClick(objectsender,EventArgsargs)
{
GetData("Previous");
}

publicstaticvoidNext_OnClick(objectsender,EventArgsargs)
{
GetData("Next");
}
}

publicclassSample
{
staticvoidMain()
{
Application.Run(newPagingSample());
}
}