欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【大数量查询分页显示 微软的解决办法】,下面是详细的分享!
大数量查询分页显示 微软的解决办法
微软的解决办法
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());
}
}
以上所分享的是关于大数量查询分页显示 微软的解决办法,下面是编辑为你推荐的有价值的用户互动:
相关问题:hibernate一对多查询怎么分页,即限制查询出的结果...
答:select * from (select rownum(cid) from student) s where s.cid >>详细
相关问题:word 2013中垂直滚动条上方少了一个可以分页显示的...
答:先问一下!把屏幕分上下两部分显示,页码不连续,你怎么实现的啊? >>详细
相关问题:win8系统中怎样查看系统的分页的大小
答:为Win8系统划分系统磁盘的空间,这是必须的,那我们安装的时候要划分多大的系统磁盘空间呢?划分多了,自然会浪费了,但是划分少了,后面慢慢的就不够用了。 微软官方建议: 安装Win8(x86) 至少需要 16 GB可用磁盘空间; 安装Win8 (X64)至少需要20... >>详细
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
