C#.NET中在数据量很大时,如何实现DATAGRID的分页,要求见文,麻烦各位前辈看看

27257674 2004-10-29 10:07:35
各位前辈:
由于数据库的记录比较多,如何实现不一次性从数据库中提取全部数据,因为这样很费时,(以致于经常超时),而改用只取一部份数据。需要时再重新取。请问如何实现!!!
   说明:该DATAGRID控件的翻页包含的形式为:
    1:可以直接输入指定页数。
    2:有上一页,下一页
    3:可以点击页数进入相应页(范围由DATAGRID的PAGESIZE属性确定)



...全文
285 点赞 收藏 8
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
sun926 2005-02-02
up
回复
ltqlyy125 2005-02-02
45476456456456456456564564564645如同依然太阳日太阳日感到佛朗哥大礼服;感到风格感到风格豆腐干豆腐干豆腐干豆腐干法乘法感地方忽然谈话
回复
mooniscrazy 2004-10-29
几种办法:
一、采用嵌套的sql返回特定页的数据,类似这样:
select top 5 * from tb where id not in (select top 10 id from tb order by id) order by id
二、采用缓存主键的方法,就是每次取n条,记下n条中最大的id为OldId,下一页的时候,取id〉OldId的前n条,此法性能最好,但是不能自由挑转.
三、google式返回法,实际上是二的变形。
四、结合一、二、三的办法。
回复
marvelstack 2004-10-29
http://blog.csdn.net/zhzuo/archive/2004/10/29/158638.aspx
http://blog.csdn.net/zhzuo/archive/2004/10/28/156647.aspx
有什么问题或好的建议请留言.
回复
roapzone 2004-10-29
自己写分页控件把
回复
yaopeng117 2004-10-29
建议参考

http://community.csdn.net/Expert/topic/3494/3494950.xml?temp=3.303164E-02
回复
Jinniu 2004-10-29
通过执行存储过程来实现分页和跳转!
回复
生活真美好 2004-10-29
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<html>
<body>
<form method=post runat="server">
<asp:Label runat="server" ID="MoreInfo" />
<br/><br/>
<asp:DataGrid runat="server" ID="Datagrid1"
DataKeyField="EmployeeID"
AutoGenerateColumns="False"
Width="100%"
OnSelectedIndexChanged="Datagrid1_SelectionChanged"
AllowPaging="true"
AllowCustomPaging="true"
PageSize="3"
PagerStyle-HorizontalAlign="Right"
PagerStyle-PageButtonCount="4"
PagerStyle-Mode="NumericPages"
PagerStyle-BackColor="Beige"
PagerStyle-ForeColor="Red"
OnPageIndexChanged="Datagrid1_PageChanged"
HeaderStyle-Font-Size="10"
HeaderStyle-Font-Bold="true"
HeaderStyle-ForeColor="Red"
HeaderStyle-BackColor="Yellow"
HeaderStyle-BorderColor="Red"
HeaderStyle-BorderWidth="5"
FooterStyle-BorderColor="Red"
FooterStyle-BorderWidth="5"
ItemStyle-BackColor="LightCyan"
ItemStyle-ForeColor="DarkBlue"
AlternatingItemStyle-BackColor="LightYellow"
AlternatingItemStyle-ForeColor="Maroon"
AlternatingItemStyle-Font-Italic="true"
SelectedItemStyle-ForeColor="Yellow"
SelectedItemStyle-BackColor="Red"
SelectedItemStyle-Font-Bold="true">
<Columns>
<asp:BoundColumn HeaderText="ID" ItemStyle-Width="30px" DataField="EmployeeID"/>
<asp:BoundColumn HeaderText="Title" ItemStyle-Width="50px" DataField="TitleOfCourtesy"/>
<asp:BoundColumn HeaderText="Last Name" ItemStyle-Width="150px" DataField="LastName"/>
<asp:BoundColumn HeaderText="First Name" DataField="FirstName"/>
<asp:ButtonColumn CommandName="Select" ItemStyle-Width="20px" Text="<img border=0 src=message.gif>" />
</Columns>
</asp:DataGrid>
</form>
</body>
</html>

<script runat="server" language="C#">
void Page_Load()
{
if (!Page.IsPostBack)
{
SetGridItemCount();
BindGrid();
}
}

void SetGridItemCount()
{
// create the command and the connection
string connString = "server=(local);database=Northwind;uid=sa;pwd=;";
string sql = "SELECT COUNT(*) FROM Employees";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);

conn.Open();
// execute the command and use the return value for the grid's VirtualItemCount prop
Datagrid1.VirtualItemCount = (int)cmd.ExecuteScalar();
conn.Close();
}

void BindGrid()
{
// create the command and the connection
string connString = "server=(local);database=Northwind;uid=sa;pwd=;";
SqlConnection conn = new SqlConnection(connString);

SqlCommand cmd = new SqlCommand("sp_GetEmployeesByPage", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@PageNumber", SqlDbType.Int, 4));
cmd.Parameters["@PageNumber"].Value = Datagrid1.CurrentPageIndex + 1;
cmd.Parameters.Add(new SqlParameter("@PageSize", SqlDbType.Int, 4));
cmd.Parameters["@PageSize"].Value = Datagrid1.PageSize;

// open the connection and get the Reader
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();

// bind the reader to the DataList
Datagrid1.DataSource = reader;
Datagrid1.DataBind();

// close the reader and the connection
reader.Close();
conn.Close();
}

protected void Datagrid1_SelectionChanged(object sender, EventArgs e)
{
// create the command and the connection
string connString = "server=(local);database=Northwind;uid=sa;pwd=;";
int empID = (int)Datagrid1.DataKeys[Datagrid1.SelectedIndex];
string sql = "SELECT * FROM Employees WHERE EmployeeID = " + empID.ToString();
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);

// open the connection and get the Reader
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();

// show the results
StringBuilder str = new StringBuilder("");
reader.Read();
str.Append("<b>");
str.Append(reader["FirstName"].ToString());
str.Append(" ");
str.Append(reader["LastName"].ToString());
str.Append("<br/>");
str.Append(reader["Title"].ToString());
str.Append("<br/>");
str.Append(reader["Address"].ToString());
str.Append("<br/>");
str.Append(reader["City"].ToString());
str.Append(", ");
str.Append(reader["Region"].ToString());
str.Append("</b><br/>");
str.Append(reader["Notes"].ToString());
MoreInfo.Text = str.ToString();

// close the reader and the connection
reader.Close();
conn.Close();

// rebind the grid
BindGrid();
}


protected void Datagrid1_PageChanged(Object sender, DataGridPageChangedEventArgs e)
{
// deselect the currently selected row, if any
Datagrid1.SelectedIndex = -1;
MoreInfo.Text = "";
// change the current page and rebind
Datagrid1.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}

</script>

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_GetEmployeesByPage]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_GetEmployeesByPage]
GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

CREATE PROCEDURE sp_GetEmployeesByPage
@PageNumber int,
@PageSize int
AS

-- create a temporary table with the columns we are interested in
CREATE TABLE #TempEmployees
(
ID int IDENTITY PRIMARY KEY,
EmployeeID int,
LastName nvarchar(20),
FirstName nvarchar(10),
Title nvarchar(30),
TitleOfCourtesy nvarchar(25),
Address nvarchar(60),
City nvarchar(15),
Region nvarchar(15),
Country nvarchar(15),
Notes ntext
)

-- fill the temp table with all the employees
INSERT INTO #TempEmployees
(
EmployeeID,
LastName,
FirstName,
Title,
TitleOfCourtesy,
Address,
City,
Region,
Country,
Notes
)
SELECT
EmployeeID,
LastName,
FirstName,
Title,
TitleOfCourtesy,
Address,
City,
Region,
Country,
Notes
FROM
Employees ORDER BY EmployeeID ASC

-- declare two variables to calculate the range of records to extract for the specified page
DECLARE @FromID int
DECLARE @ToID int
-- calculate the first and last ID of the range of records we need
SET @FromID = ((@PageNumber - 1) * @PageSize) + 1
SET @ToID = @PageNumber * @PageSize

-- select the page of records
SELECT * FROM #TempEmployees WHERE ID >= @FromID AND ID <= @ToID
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

回复
相关推荐
发帖
C#
创建于2007-09-28

10.5w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2004-10-29 10:07
社区公告

让您成为最强悍的C#开发者