1
这是存储过程.加入SQL数据库中
2
CREATE PROCEDURE [spGet_Customers_By_Page]
3
@CurrentPage int,
4
@PageSize int,
5
@TotalRecs int output
6
AS
7
-- 创建一个 temp table 以暂存所请求的 Customers 表数据.
8
--由于Custormers表并没有Identity列来对所有记录进行顺序编号,
9
--因此需要在临时表中增加一个Identity列.
10
CREATE TABLE #TempTable
11
(
12
ID int IDENTITY PRIMARY KEY,
13
CompanyName nvarchar(40),
14
ContactName nvarchar(30),
15
ContactTitle nvarchar(30),
16
Phone nvarcha*(24),
17
Fax nvarchar(24),
18
)
19
--选择了Customers表的所有行(仅限于所需要的列),并将之填充入临时表.
20
INSERT INTO #TempTable
21
(CompanyName , ContactName, ContactTitle, Phone, Fax)
22
SELECT CompanyName , ContactName, ContactTitle, Phone, Fax
23
FROM Customers
24
--利用@CurrentPage和@PageSize变量,可以确定所选的第一条和最后一条纪录.
25
DECLARE @FirstRec int ,@LastRec int
26
SELECT @FirstRec = (@CurrentPage - 1) * @PageSize //这里的firstRec关不代表当前页首条纪录的id值,而是比id值少一的最近值.
27
SELECT @LastRec = (@CurrentPage * @PageSize +1) //这里的LastRec关不代表当前页首条纪录的id值,而是比id值大一的最近值
28
//为什么要写成这样,当然有它的原因,因为只有这样才不会缺失首页的首条纪录.
29
30
--通过select查询语句确定返回到web表单的一页数据.
31
SELECT CompanyName , ContactName, ContactTitle, Phone, Fax
32
FROM #TempTable
33
WHERE ID > @FirstRec AND ID < @LastRec
34
35
--获得Customers表的所有记录数,并将记录数 (COUNT*) 赋给@TotalRec参数值返回.
36
SELECT @TotalRecs = COUNT(*) FROM Customers
37
38
2后台代码:
39
using System;
40
using System.Collections;
41
using System.ComponentModel;
42
using System.Data;
43
using System.Drawing;
44
using System.Web;
45
using System.Web.SessionState;
46
using System.Web.UI;
47
using System.Web.UI.WebControls;
48
using System.Data.SqlClient;
49
namespace changedpage
50
{
51
/// <summary>
52
/// WebForm1 的摘要说明。
53
/// </summary>
54
public class WebForm1 : System.Web.UI.Page
55
{
56
protected System.Web.UI.WebControls.Label CurrentPage;
57
protected System.Web.UI.WebControls.LinkButton FirstPage;
58
protected System.Web.UI.WebControls.LinkButton PreviousPage;
59
protected System.Web.UI.WebControls.LinkButton NextPage;
60
protected System.Web.UI.WebControls.LinkButton LastPage;
61
protected System.Web.UI.WebControls.DataGrid myDataGrid;
62
protected int CurrentPageNumber;
63
protected System.Web.UI.WebControls.Label TotalPages;
64
protected SqlConnection myconnection = new SqlConnection();
65
66
private void Page_Load(object sender, System.EventArgs e)
67
{
68
// 在此处放置用户代码以初始化页面
69
70
if(!Page.IsPostBack)
71
{
72
CurrentPageNumber=1;
73
binddata();
74
}
75
}
76
protected void binddata()
77
{
78
string strconn = "server=localhost;user id=sa;password=;database=Northwind " ;
79
myconnection.ConnectionString= strconn;
80
SqlCommand mycommand = new SqlCommand("spGet_Customers_By_Page",myconnection);
81
//设置为存储过程
82
mycommand.CommandType = CommandType.StoredProcedure;
83
mycommand.Parameters.Add(new SqlParameter("@CurrentPage",SqlDbType.Int));
84
mycommand.Parameters["@CurrentPage"].Value=CurrentPageNumber;
85
mycommand.Parameters.Add(new SqlParameter("@PageSize",SqlDbType.Int));
86
mycommand.Parameters["@PageSize"].Value=myDataGrid.PageSize;
87
mycommand.Parameters.Add(new SqlParameter("@TotalRecs",SqlDbType.Int));
88
mycommand.Parameters["@TotalRecs"].Direction=ParameterDirection.Output;
89
try
90
{
91
myconnection.Open();
92
SqlDataReader myreader = mycommand.ExecuteReader();
93
myDataGrid.DataSource=myreader;
94
myDataGrid.DataBind();
95
}
96
catch( Exception ex)
97
{
98
Response.Write(ex.ToString());
99
}
100
finally
101
{
102
myconnection.Close();
103
}
104
CurrentPage.Text = CurrentPageNumber.ToString();
105
if(!Page.IsPostBack)
106
{
107
int Total_Records = ((int) mycommand.Parameters["@TotalRecs"].Value);
108
decimal Total_Pages = decimal.Parse(Total_Records.ToString())/myDataGrid.PageSize;
109
110
TotalPages.Text=(System.Math.Ceiling(double.Parse(Total_Pages.ToString()))).ToString();
111
}
112
if(CurrentPageNumber ==1)
113
{
114
PreviousPage.Enabled=false;
115
NextPage.Enabled=true;
116
}
117
else
118
{
119
if(CurrentPageNumber == Int32.Parse(TotalPages.Text))
120
{
121
PreviousPage.Enabled=true;
122
NextPage.Enabled=false;
123
}
124
else
125
{
126
PreviousPage.Enabled=true;
127
NextPage.Enabled=true;
128
}
129
}
130
}
131
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
浙公网安备 33010602011771号