首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

分享小弟我的百万级数据查询分页

2012-01-19 
分享我的百万级数据查询分页有什么不好的地方欢迎指点,主要运用sql2005的ROW_NUMBER()分页其实很简单,只要

分享我的百万级数据查询分页

有什么不好的地方欢迎指点,主要运用sql2005的ROW_NUMBER()
分页其实很简单,只要把页参数传递到以下SQL语句中即可。
where number between cast('" & PageSize & "' as int) *(cast('" & CurrentPage & "' as int)-1)+1 and cast('" & PageSize & "' as int)*cast('" & CurrentPage & "' as int)")
如果不懂vb.net可以到这个网页把代码转换为c# http://www.developerfusion.com/tools/convert/vb-to-csharp/
前台用 gridview repeater datalist 都是一样的。


前台

HTML code
            <asp:ScriptManager ID="ScriptManager1" runat="server">                </asp:ScriptManager>                <asp:UpdatePanel ID="UpdatePanel1" runat="server">                                     <ContentTemplate>            <asp:repeater id="Repeater1" runat="server">                <HeaderTemplate>                    <table id="tb01" width="260%" border="0" cellspacing="1" cellpadding="2" bgcolor="#dddddd">                        <tbody>                            <tr style="background-color:#ffffff">                                <td class="trTitle"  style="width:80px;background-image:url(images/trbg02.gif);background-color:#ffffff;height:23px;" >編號</td>                                <td class="trTitle"  style="width:80px;background-image:url(images/trbg02.gif);background-color:#ffffff;height:23px;">姓名</td>                                <td class="trTitle"   style="width:150px;background-image:url(images/trbg02.gif);background-color:#ffffff;height:23px;">名稱</td>                                                            </tr>                </HeaderTemplate>                <ItemTemplate>                    <tr style="background-color:#ffffff">                        <td height="20px"  align="left">                            <a style="cursor:hand" href="<%= strLinkManitCrsCat %>&recid=<%# Container.DataItem("rec_id") %>&mode=1"  title="編輯/刪除"><strong><asp:label  Font-Names="Tahoma" id="lblClassNum" Width="80px" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"emp_id") %>'>                            </asp:label></strong></a>                        </td>                        <td align="left">                            <asp:label id="Label4" Width="80px" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"emp_chn_nam") %>'>                            </asp:label>                        </td>                        <td align="left">                            <asp:label id="lblCrsCatDesc" Width="150px" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"crs_num") %>'>                            </asp:label>                        </td>                                            </tr>                </ItemTemplate>                <AlternatingItemTemplate>                    <tr bgcolor="#f9f9f9">                        <td height="20px"  align="left">                            <a style="cursor:hand" href="<%= strLinkManitCrsCat %>&recid=<%# Container.DataItem("rec_id") %>&mode=1" title="編輯/刪除"><strong><asp:label Font-Names="Tahoma" id="lblClassNum" Width="80px" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"emp_id") %>'>                            </asp:label></strong></a>                        </td>                        <td align="left">                            <asp:label id="Label4" Width="80px" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"emp_chn_nam") %>'>                            </asp:label>                        </td>                    <td align="left">                            <asp:label id="lblCrsCatDesc" Width="150px" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"crs_num") %>'>                            </asp:label>                        </td>                        <td align="left">                            <asp:label id="Label8" Width="60px" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"cls_num") %>'>                            </asp:label>                        </td>                                                                                            </tr>                </AlternatingItemTemplate>                <FooterTemplate>                    <tr bgcolor="#f2f2f2">                        <td height="8" colspan="22" align="center"></td>                    </tr>                    </tbody></table>                </FooterTemplate>            </asp:repeater>            <div style="PADDING-TOP:10px" align="center">                            &nbsp; 共<asp:label id="LPageCount" ForeColor="#ff0000" Runat="server"></asp:label>頁            <asp:label id="LTotalCount" ForeColor="#ff0000" Runat="server"></asp:label>條記錄                    <asp:linkbutton id="Fistpage" Runat="server" CommandName="0">首頁</asp:linkbutton>                    <asp:linkbutton id="Prevpage" Runat="server" CommandName="prev">上一頁</asp:linkbutton>                    <asp:linkbutton id="Nextpage" Runat="server" CommandName="next">下一頁</asp:linkbutton>                    <asp:linkbutton id="Lastpage" Runat="server" CommandName="last">尾頁</asp:linkbutton>當前第                    <asp:label id="LCurrentPage" ForeColor="#ff0000" Runat="server"></asp:label>頁           &nbsp; 轉到第           <asp:textbox id="gotoPage" Width="30px" Runat="server" AutoPostBack="True" MaxLength="5" ></asp:textbox>頁           <asp:Label style=" POSITION: absolute" id="msgbox" runat="server" ForeColor="Red" BorderColor="Red"></asp:Label>           </div>                      </ContentTemplate>                </asp:UpdatePanel>            <input type="hidden" id="sortfield" runat="server" name="sortfield"/> <input type="hidden" id="sortstring" runat="server" name="sortstring"/>&nbsp;        </form>    </body></html> 






[解决办法]
up,收藏
[解决办法]

[解决办法]
不错 收藏中
[解决办法]
数据库引擎给我们实现了所有功能,剩下的就是看我们怎么使用他了。

赞楼主的分享精神!
[解决办法]
谢谢楼主
[解决办法]
收藏啊!
[解决办法]
支持
[解决办法]
学习!
[解决办法]
谢谢...学习下
[解决办法]

[解决办法]
我来接分 至于分页 哈哈 本人还要sql存储过程
[解决办法]
用linq比较方便
skip().take()
大法就ok了 

从本质上来说 是一样的
[解决办法]
效率也应该一般,不会太高
[解决办法]
不爽,还是VB的,不过先收藏了
[解决办法]
打个标记先,再仔细研究!
[解决办法]
赞一个,代码界需要楼主这样地人才!!!
[解决办法]
探讨
spnetPager应该只是个分页查询,没有针对大数据量做优化

[解决办法]
protected void BindData() { 
initdb(); 
OleDbDataAdapter sda = new OleDbDataAdapter("select * from t_product",conn);
DataSet ds = new DataSet(); 
//sda.Fill(ds, 10, 10, "temptbl"); 
sda.Fill(ds, pager1.PageSize * (pager1.CurrentPageIndex - 1), pager1.PageSize, "temptbl"); 
DataList1.DataSource = ds.Tables["temptbl"]; 
DataList1.DataBind(); 

AspNetPager是把所有数据取出来,再取一部分到gridview显示的。 
虽然分页效率也不错,但是把所有数据都读出来就浪费了服务器资源了。

错了,不知道你工作多少年了, AspNetPager 是跟数据没有任何的关系的, 只跟你传给总共有多少条数据,一页显示几条。是一个非常好的翻页控件,你这个,我刚毕业那阵子也是这么做程序的。没有任何可重复用性。
[解决办法]
Pager.ascx
<%@ control language="c#" inherits="Troika.Ascx.Pager" CodeFile="~/Ascx/Pager.ascx.cs"%>
<script type="text/javascript">
function callButtonEvent()
{
var keycode =window.event.keyCode;
if(keycode==13)
{
if(check()==true)
{
event.cancelBubble=true;
event.returnValue=false;
document.getElementById('<%=btnGo.ClientID%>').click();
}


function check()
{
var count = parseInt(document.getElementById('<%=lblTotal.ClientID%>').outerText);
var txt = document.getElementById('<%=txtCurrentPage.ClientID%>').value;
var cur = parseInt(txt);
if ((cur | NaN) ==0) 
{
alert('Input page must format as integer.');
event.cancelPostBack=true;
return false;
}


if (cur > count || cur < 1)
{
alert('Input page no out of range.');
event.cancelPostBack=true;
return false;
}
}
</script>
<table class="tablepagefoot" align="right">
<tr>
<td>
页次&nbsp;<asp:literal id="lipagenum" Runat="server" />/<asp:literal id="lipageallnum" Runat="server" />&nbsp; 
每页<asp:literal id="lipagesize" Runat="server" />条&nbsp; 共<asp:literal id="lirownum" Runat="server" />条&nbsp;
<asp:LinkButton id="btnFirstPage" runat="server" CausesValidation="False" CommandArgument="First">首页</asp:LinkButton>&nbsp;
<asp:LinkButton id="btnPrevPage" runat="server" CausesValidation="False" CommandArgument="Prev">上一页</asp:LinkButton>&nbsp;
<asp:LinkButton id="btnNextPage" runat="server" CausesValidation="False" CommandArgument="Next">下一页</asp:LinkButton>&nbsp;
<asp:LinkButton id="btnLastPage" runat="server" CausesValidation="False" CommandArgument="Last">尾页</asp:LinkButton>&nbsp;&nbsp;<ASP:TEXTBOX ID="txtCurrentPage" RUNAT="server" MAXLENGTH="5" Width="35px">0</ASP:TEXTBOX>
<input type="hidden" runat="server" id="lblTotal" />
<ASP:BUTTON ID="btnGo" RUNAT="server" SkinId="btngoto" CausesValidation="False" COMMANDARGUMENT="Go" ToolTip="转到"></ASP:BUTTON>&nbsp;</td>
</tr>
</table>

[解决办法]
Pager.ascx.cs
namespace Troika.Ascx
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
//using Service;

/// <summary>
///page 的摘要说明。
/// </summary>
public partial class Pager : System.Web.UI.UserControl
{
int size=10;//可以在web.config中配置
public event System.EventHandler NavigationClick;

protected void Page_Load(object sender, System.EventArgs e)
{
this.txtCurrentPage.Attributes.Add("onkeypress","callButtonEvent();");
this.btnGo.Attributes.Add("onclick","check();");
if(!this.IsPostBack)
{
SetStyle();
SetEnable();
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
this.btnFirstPage.Click += new System.EventHandler(this.btnGo_Click);
this.btnPrevPage.Click += new System.EventHandler(this.btnGo_Click);
this.btnNextPage.Click += new System.EventHandler(this.btnGo_Click);
this.btnLastPage.Click += new System.EventHandler(this.btnGo_Click);
this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
}

/// <summary>
///Required method for Designer support - do not modify
///the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{

}
#endregion

#region btnGo_Click
public void btnGo_Click(object sender, System.EventArgs e)
{
LinkButton linkbtn = sender as LinkButton;
if( null == linkbtn )//button
{
Button btn = sender as Button;
if( null == btn )


{
return;
}
else
{
int selPage = -1;
try
{
selPage =Int32.Parse(txtCurrentPage.Text);
}
catch
{
selPage = -1;
}
if (selPage > 0 && selPage <= PageCount)
{
ViewState["CurrentPageIndex"] = selPage;
}
else
{
return;
}
}
}
else//linkbutton
{
switch ( linkbtn.CommandArgument.Trim() )
{
case "First":
ViewState["CurrentPageIndex"] = 1;
break;
case "Prev":
ViewState["CurrentPageIndex"] = (CurrentPageIndex > 1) ? CurrentPageIndex - 1 : 1;
break;
case "Next":
ViewState["CurrentPageIndex"] = (PageCount > CurrentPageIndex) ? CurrentPageIndex + 1 : PageCount;
break;
case "Last":
ViewState["CurrentPageIndex"] = PageCount;
break;
default:
break;
}
}
SetEnable();//设置显示样式
if (NavigationClick!=null)//调用事件 
{
NavigationClick( sender,e );
}
}
#endregion

#region SetStyle
private void SetStyle()
{
this.btnFirstPage.Attributes["style"] = "CURSOR: hand";
this.btnLastPage.Attributes["style"] = "CURSOR: hand";
this.btnNextPage.Attributes["style"] = "CURSOR: hand";
this.btnPrevPage.Attributes["style"] = "CURSOR: hand";
}
#endregion

#region SetEnable
// 应根据当前的CurrentPageIndex和pageCount设定哪些按钮可用
public void SetEnable()
{
this.lblTotal.Value = PageCount.ToString();

txtCurrentPage.Text =CurrentPageIndex.ToString();

btnPrevPage.Enabled = false;
btnNextPage.Enabled = false;

if( PageCount >1 )
{
btnFirstPage.Enabled = btnPrevPage.Enabled = ( CurrentPageIndex >1 );
btnNextPage.Enabled = btnLastPage.Enabled = ( CurrentPageIndex < PageCount );
}
else
{
btnFirstPage.Enabled = false;
btnLastPage.Enabled = false;
btnPrevPage.Enabled = false;
btnNextPage.Enabled = false;
}

lipagenum.Text = CurrentPageIndex.ToString();
lipageallnum.Text = PageCount.ToString();
lipagesize.Text = PageSize.ToString();
lirownum.Text = RecordCount.ToString();
 
}

#endregion

#region Property
//获取或设置当前显示页的索引。
public int CurrentPageIndex
{
get
{
object cpage=ViewState["CurrentPageIndex"];
int pindex=(cpage==null)?1:(int)cpage;
//if(pindex>PageCount&&PageCount>0)
// return PageCount;
//else if(pindex<1)
// return 1;
return pindex;
}
set
{
int cpage=value;
if(cpage<1)
cpage=1;
//else if(cpage>this.PageCount)
//cpage=this.PageCount;
ViewState["CurrentPageIndex"]=cpage;
}
}
// 获取或设置需要分页的所有记录的总数。
public int RecordCount
{
get
{
object obj=ViewState["Recordcount"];
return (obj==null)?0:(int)obj;
}
set
{
ViewState["Recordcount"]=value;
SetEnable();
}
}

//获取当前页之后的页的总数。
public int PagesRemain
{
get
{
return PageCount-CurrentPageIndex;
}
}
// 获取或设置每页显示的项数。
public int PageSize
{
get
{
object obj=ViewState["PageSize"];


if (obj==null)
{
obj= size;
}
return (obj==null)?size:(int)obj;
}
set
{
int pageSize = value;

if (Math.Abs(pageSize) == 0)
pageSize = size;

ViewState["PageSize"]=pageSize;
}
}

// 获取在当前页之后还未显示的剩余记录的项数。
public int RecordsRemain
{
get
{
if(CurrentPageIndex<PageCount)
{
return RecordCount-(CurrentPageIndex*PageSize);
}
else
{
return 0;
}
}
}

// 获取所有要分页的记录需要的总页数。
public int PageCount
{
get{return (RecordCount > 0) ? (int)Math.Ceiling((double)RecordCount/(double)PageSize) : 1;}
}

public int XRecord
{
get
{
return int.Parse( System.Configuration.ConfigurationSettings.AppSettings["XRecord"].Trim() );
}
}
#endregion Property
}
}

[解决办法]
坚决收藏~决不手软
[解决办法]
学习学习
[解决办法]
打个标记先,再研究! 呵呵
[解决办法]
太NB了吧,我日了,我以前还是去找了一个存储过程,试一下这个。
[解决办法]
用between效果很高吗?

热点排行