通用SQL分页程序
近来在网上找了一些分页程序,大部分都是基于SQL存储过程的,在实现上有一定的局限性,其中有一片文章我比较喜欢是基于SQL命令的,但是在实现代码有一点点不合理,我做了一些改进放上来给大家看看,希望能帮得上大家。
1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DefaultDataGrid_02.aspx.cs" Inherits="数据分页DEMO_DefaultDataGrid" %>
2
3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5
<html xmlns="http://www.w3.org/1999/xhtml" >
6
<head runat="server">
7
<title>无标题页</title>
8
</head>
9
<body>
10
<form id="form1" runat="server">
11
<div>
12
<asp:LinkButton id="lbtnFirst" Font-Size="Smaller" Runat="server" OnClick="lbtnFirst_Click">首頁</asp:LinkButton>
13
<asp:LinkButton id="lbtnBack" Font-Size="Smaller" Runat="server" OnClick="lbtnBack_Click">上頁</asp:LinkButton>
14
<asp:LinkButton id="lbtnNext" Font-Size="Smaller" Runat="server" OnClick="lbtnNext_Click">下頁</asp:LinkButton>
15
<asp:LinkButton id="lbtnLast" Font-Size="Smaller" Runat="server" OnClick="lbtnLast_Click">尾頁</asp:LinkButton>
16
<asp:Label id="Label1" Font-Size="Smaller" runat="server">当前页:</asp:Label>
17
<asp:Label id="lblCurrentPage" Font-Size="Smaller" runat="server">1</asp:Label>
18
<asp:Label id="Label2" Font-Size="Smaller" runat="server">总页:</asp:Label>
19
<asp:Label id="lblPageCount" Font-Size="Smaller" runat="server">200</asp:Label>
20
<asp:Label id="Label3" Font-Size="Smaller" runat="server">跳转:</asp:Label>
21
<asp:TextBox id="txtToPage" Font-Size="Smaller" runat="server" Width="88px"></asp:TextBox>
22
<asp:Button id="btnToPage" Font-Size="Smaller" runat="server" Text="go" OnClick="btnToPage_Click"></asp:Button>
23
24
<asp:DataGrid id="DataGrid1" runat="server" CellPadding="4" ForeColor="#333333"
25
GridLines="None" Font-Bold="False" Font-Italic="False"
26
Font-Names="幼圆" Font-Overline="False" Font-Size="Smaller"
27
Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Left">
28
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
29
<EditItemStyle BackColor="#7C6F57" />
30
<SelectedItemStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
31
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
32
<AlternatingItemStyle BackColor="White" />
33
<ItemStyle BackColor="#E3EAEB" />
34
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
35
</asp:DataGrid>
36
37
38
</div>
39
</form>
40
</body>
41
</html>
42

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

1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11
using System.Data.SqlClient;
12
using System.Text;
13
public partial class 数据分页DEMO_DefaultDataGrid : System.Web.UI.Page
14
{
15
protected void Page_Load(object sender, EventArgs e)
16
{
17
if (!Page.IsPostBack)
18
{
19
ShowData(Convert.ToInt64(this.lblCurrentPage.Text),"报关进度表","运单号","ASC",10);
20
}
21
}
22
private void ShowData(long page,string tableName,string fieldName,
23
string orderString,int GridSize)
24
{
25
26
27
/**/
28
/*-------------------设置参数------------------*/
29
//指定要排序的表
30
//比如报关进度表
31
string tblName = tableName;
32
//指定要排序的字段
33
//比如"运单号,收货公司"
34
string fldName = fieldName;
35
//指定要使用的排序方式
36
//比如ASC DES
37
string orderStr = orderString;
38
//指定分页大小
39
int PageSize = GridSize;
40
41
/**/
42
/*-------------------设置结束------------------*/
43
44
string conStr = ConfigurationManager.AppSettings["SQLDB"].ToString();
45
46
47
48
SqlConnection conn = new SqlConnection(conStr);
49
conn.Open();
50
SqlCommand cmd = new SqlCommand("select count(*) from " + tblName, conn);
51
//得到总页数
52
lblPageCount.Text = Convert.ToString(((int)cmd.ExecuteScalar() / PageSize + 1));
53
54
//排除的记录部分
55
StringBuilder TopSql = new StringBuilder();
56
TopSql.AppendFormat("select top {0} {1} from {2} order by {3} {4}",
57
Convert.ToString((page - 1) * PageSize),
58
fldName, tblName, fldName,orderStr
59
);
60
//得到排除记录里的最大ID号
61
StringBuilder MaxSql = new StringBuilder();
62
MaxSql.AppendFormat("select max({0}) from ({1}) as t",fldName,TopSql.ToString());
63
//得到最终结果
64
StringBuilder sql = new StringBuilder();
65
sql.AppendFormat("select top {0} * from {1} where {2}>({3}) order by {4} {5}",
66
PageSize.ToString(), tblName, fldName, MaxSql.ToString(), fldName,
67
orderStr
68
);
69
70
71
if (page == 1)
72
{
73
sql = new StringBuilder();
74
sql.AppendFormat("select top {0} * from {1}",
75
PageSize,tblName,lblCurrentPage.Text);
76
}
77
78
try
79
{
80
SqlDataAdapter da = new SqlDataAdapter(sql.ToString(), conn);
81
System.Data.DataSet ds = new DataSet();
82
da.Fill(ds);
83
DataGrid1.DataSource = ds.Tables[0].DefaultView;
84
DataGrid1.DataBind();
85
86
conn.Close();
87
88
}
89
catch (Exception ex)
90
{
91
Response.Write(ex.Message.ToString());
92
}
93
94
}
95
96
97
protected void lbtnFirst_Click(object sender, EventArgs e)
98
{
99
lblCurrentPage.Text = "1";
100
ShowData(1, "报关进度表", "运单号", "ASC", 10);
101
102
}
103
protected void lbtnBack_Click(object sender, EventArgs e)
104
{
105
if (lblCurrentPage.Text != "1")
106
{
107
lblCurrentPage.Text = Convert.ToString(Convert.ToInt64(lblCurrentPage.Text) - 1);
108
ShowData(Convert.ToInt64(lblCurrentPage.Text), "报关进度表", "运单号", "ASC", 10);
109
}
110
}
111
protected void lbtnNext_Click(object sender, EventArgs e)
112
{
113
if (lblCurrentPage.Text != lblPageCount.Text)
114
{
115
lblCurrentPage.Text = Convert.ToString(Convert.ToInt64(lblCurrentPage.Text) + 1);
116
ShowData(Convert.ToInt64(lblCurrentPage.Text), "报关进度表", "运单号", "ASC", 10);
117
}
118
119
}
120
protected void lbtnLast_Click(object sender, EventArgs e)
121
{
122
lblCurrentPage.Text = lblPageCount.Text;
123
ShowData(Convert.ToInt64(lblPageCount.Text), "报关进度表", "运单号", "ASC", 10);
124
}
125
protected void btnToPage_Click(object sender, EventArgs e)
126
{
127
if (Convert.ToInt64(txtToPage.Text.Trim()) > 0 && Convert.ToInt64(txtToPage.Text.Trim()) < Convert.ToInt64(lblPageCount.Text))
128
{
129
lblCurrentPage.Text = txtToPage.Text;
130
ShowData(Convert.ToInt64(txtToPage.Text),"报关进度表", "运单号", "ASC", 10);
131
}
132
133
}
134
}
135

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

132

133

134

135

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述