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

ASP.NET
ASP.NET下MVC设计模式的实现
ASP.NET中利用cookies保持客户端信息
有关TextBox中输入字符控制的一种解决办法
ASP.NET访问Oracle数据库的方法
追踪ASP.NET代码里的bug
在asp.net中为Web用户控件添加属性和事件
使用.NET实现断点续传
ASP.NET Whidbey中实现Provider
使用函数传递参数来执行数据库操作
ASP.NET中实现模版的动态加载
用Repeater控件显示数据
五种常见的ASP.NET安全缺陷
利用Treeview实现树形列表
将Asp.net页面输出为HTML
不走寻常路 设计ASP.NET应用程序的七大绝招
ASP.NET中的HTTP模块和处理程序
充分利用ASP.NET的三种缓存提高站点性能
在ASP.NET中使用Treeview控件和XML
ASP.NET+XML Web服务客户端创建Web服务
ASP.NET中不定级动态菜单的实现

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 62 ::
收藏到网摘: 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());
}
}