几种办法:
一、采用嵌套的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式返回法,实际上是二的变形。
四、结合一、二、三的办法。
一、采用嵌套的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式返回法,实际上是二的变形。
四、结合一、二、三的办法。
解决方案 »
- 求助,Access2007数据库,窗体程序,如何获取datagridview里面表最大的ID(主键自动编号),然后向表中添加数据时,将ID
- VS2010 using System.Runtime.InteropServices 无效
- 控件问题
- wince平台下的摄像头应用程序问题
- socket发送文件,服务器端如何得到所发送文件的文件名?
- 把EXCEL表格里的数据对应的导入到数据库
- c#winform中一个cs文件中的按钮,如何调用另一个cs文件中的第三方控件radioGroup的EditValueChanged事件
- 关于C#下DataGrid经常出现大红叉的问题!!
- 关于windows窗体中DataGrid样式的问题
- .NET什么时候能火起来呀,我要找工作!
- 如何将一个从数据库中查询出的数字值绑定到Label?
- 新年散分之三
<%@ 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]
GOSET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GOCREATE 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
http://blog.csdn.net/zhzuo/archive/2004/10/28/156647.aspx
有什么问题或好的建议请留言.