首先是数据访问层的代码:
1
using
System;
2
using
System.Data;
3
using
System.Data.SqlClient;
4
using
System.Configuration;
5
6
namespace
WebTest.Common
7
{
8
/**/
///
<summary>
9
///
COperator 的摘要说明。
10
///
</summary>
11
public
class
COperator
12
{
13
public
COperator()
14
{
15
//
16
//
TODO: 在此处添加构造函数逻辑
17
//
18
}
19
public
DataSet HaveParameter(
string
StrProcedure)
20
{
21
SqlConnection MyConnection
=
new
SqlConnection(ConfigurationSettings.AppSettings[
"
ConnectionSqlServer
"
]);
22
SqlCommand MyCommand
=
new
SqlCommand();
23
MyCommand.Connection
=
MyConnection;
24
MyCommand.CommandType
=
CommandType.StoredProcedure;
25
MyCommand.CommandText
=
StrProcedure;
26
27
28
SqlDataAdapter MyAdapter
=
new
SqlDataAdapter();
29
30
MyAdapter.SelectCommand
=
MyCommand;
31
32
DataSet Ds
=
new
DataSet();
33
34
if
(MyConnection.State
==
System.Data.ConnectionState.Closed)
35
{
36
MyConnection.Open();
37
}
38
//
启动一个事务
39
SqlTransaction MyTrans
=
MyConnection.BeginTransaction();
40
41
//
initialize command object
42
MyCommand.Transaction
=
MyTrans;
43
44
try
45
{
46
//
进行数据库操作
47
MyAdapter.Fill(Ds);
48
MyTrans.Commit();
49
}
50
catch
51
{
52
MyTrans.Rollback();
53
}
54
finally
55
{
56
MyConnection.Close();
57
}
58
59
return
Ds;
60
}
61
62
public
void
NoReturnFunction(
string
StrParameter,
string
StrField,
string
StrProcedure)
63
{
64
//
创建数据库连接和命令的对象
65
SqlConnection myConnection
=
new
SqlConnection(ConfigurationSettings.AppSettings[
"
ConnectionSqlServer
"
]);
66
67
SqlCommand myCommand
=
new
SqlCommand(StrProcedure, myConnection);
68
69
//
指明Sql命令的操作类型是使用存储过程
70
myCommand.CommandType
=
CommandType.StoredProcedure;
71
72
SqlParameter parameterStr
=
new
SqlParameter(StrField, SqlDbType.VarChar,
500
);
73
parameterStr.Value
=
StrParameter;
74
myCommand.Parameters.Add(parameterStr);
75
76
//
打开数据库连接
77
if
(myConnection.State
==
System.Data.ConnectionState.Closed)
78
{
79
myConnection.Open();
80
}
81
82
//
启动一个事务
83
SqlTransaction MyTrans
=
myConnection.BeginTransaction();
84
85
//
initialize command object
86
myCommand.Transaction
=
MyTrans;
87
88
try
89
{
90
//
进行数据库操作
91
myCommand.ExecuteNonQuery();
92
MyTrans.Commit();
93
}
94
catch
95
{
96
MyTrans.Rollback();
97
}
98
finally
99
{
100
//
关闭数据库连接
101
myConnection.Close();
102
}
103
}
104
105
//
2参数
106
public
void
NoReturnFunction(
string
StrParameter1,
string
StrField1,
string
StrParameter2,
string
StrField2,
string
StrProcedure)
107
{
108
//
创建数据库连接和命令的对象
109
SqlConnection myConnection
=
new
SqlConnection(ConfigurationSettings.AppSettings[
"
ConnectionSqlServer
"
]);
110
111
SqlCommand myCommand
=
new
SqlCommand(StrProcedure, myConnection);
112
113
//
指明Sql命令的操作类型是使用存储过程
114
myCommand.CommandType
=
CommandType.StoredProcedure;
115
116
SqlParameter parameterStr1
=
new
SqlParameter(StrField1, SqlDbType.NVarChar,
50
);
117
parameterStr1.Value
=
StrParameter1;
118
myCommand.Parameters.Add(parameterStr1);
119
120
SqlParameter parameterStr2
=
new
SqlParameter(StrField2, SqlDbType.NVarChar,
50
);
121
parameterStr2.Value
=
StrParameter2;
122
myCommand.Parameters.Add(parameterStr2);
123
124
//
打开数据库连接
125
if
(myConnection.State
==
System.Data.ConnectionState.Closed)
126
{
127
myConnection.Open();
128
}
129
130
//
启动一个事务
131
SqlTransaction MyTrans
=
myConnection.BeginTransaction();
132
133
//
initialize command object
134
myCommand.Transaction
=
MyTrans;
135
136
try
137
{
138
//
进行数据库操作
139
myCommand.ExecuteNonQuery();
140
MyTrans.Commit();
141
}
142
catch
143
{
144
MyTrans.Rollback();
145
}
146
finally
147
{
148
//
关闭数据库连接
149
myConnection.Close();
150
}
151
152
}
153
}
154
}
155

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

136

137



138

139

140

141

142

143



144

145

146

147



148

149

150

151

152

153

154

155

Add:
1
using
System;
2
using
System.Collections;
3
using
System.ComponentModel;
4
using
System.Data;
5
using
System.Drawing;
6
using
System.Web;
7
using
System.Web.SessionState;
8
using
System.Web.UI;
9
using
System.Web.UI.WebControls;
10
using
System.Web.UI.HtmlControls;
11
12
namespace
WebTest
13
{
14
/**/
///
<summary>
15
///
AddInfo 的摘要说明。
16
///
</summary>
17
public
class
AddInfo : System.Web.UI.Page
18
{
19
protected
System.Web.UI.WebControls.Button BtOk;
20
protected
System.Web.UI.WebControls.TextBox TextBox1;
21
22
private
void
Page_Load(
object
sender, System.EventArgs e)
23
{
24
//
在此处放置用户代码以初始化页面
25
}
26
27
Web 窗体设计器生成的代码
#region
Web 窗体设计器生成的代码
28
override
protected
void
OnInit(EventArgs e)
29
{
30
//
31
//
CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
32
//
33
InitializeComponent();
34
base
.OnInit(e);
35
}
36
37
/**/
///
<summary>
38
///
设计器支持所需的方法 - 不要使用代码编辑器修改
39
///
此方法的内容。
40
///
</summary>
41
private
void
InitializeComponent()
42
{
43
this
.BtOk.Click
+=
new
System.EventHandler(
this
.BtOk_Click);
44
this
.Load
+=
new
System.EventHandler(
this
.Page_Load);
45
46
}
47
#endregion
48
49
private
void
BtOk_Click(
object
sender, System.EventArgs e)
50
{
51
WebTest.Common.COperator AddObj
=
new
WebTest.Common.COperator();
52
AddObj.NoReturnFunction(TextBox1.Text,
"
@StrTValues
"
,
"
TS_AddValuesIntoTb
"
);
53
}
54
55
}
56
}
57

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

Edit/Delete with DataGrid:
1
using
System;
2
using
System.Collections;
3
using
System.ComponentModel;
4
using
System.Data;
5
using
System.Drawing;
6
using
System.Web;
7
using
System.Web.SessionState;
8
using
System.Web.UI;
9
using
System.Web.UI.WebControls;
10
using
System.Web.UI.HtmlControls;
11
12
namespace
WebTest
13
{
14
/**/
///
<summary>
15
///
_Default 的摘要说明。
16
///
</summary>
17
public
class
_Default : System.Web.UI.Page
18
{
19
protected
System.Web.UI.WebControls.DataGrid DataGrid1;
20
21
private
void
Page_Load(
object
sender, System.EventArgs e)
22
{
23
//
在此处放置用户代码以初始化页面
24
if
(
!
IsPostBack)
25
{
26
MyDataBind();
27
}
28
}
29
30
Web 窗体设计器生成的代码
#region
Web 窗体设计器生成的代码
31
override
protected
void
OnInit(EventArgs e)
32
{
33
//
34
//
CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
35
//
36
InitializeComponent();
37
base
.OnInit(e);
38
}
39
40
/**/
///
<summary>
41
///
设计器支持所需的方法 - 不要使用代码编辑器修改
42
///
此方法的内容。
43
///
</summary>
44
private
void
InitializeComponent()
45
{
46
this
.DataGrid1.CancelCommand
+=
new
System.Web.UI.WebControls.DataGridCommandEventHandler(
this
.DataGrid1_CancelCommand);
47
this
.DataGrid1.EditCommand
+=
new
System.Web.UI.WebControls.DataGridCommandEventHandler(
this
.DataGrid1_EditCommand);
48
this
.DataGrid1.UpdateCommand
+=
new
System.Web.UI.WebControls.DataGridCommandEventHandler(
this
.DataGrid1_UpdateCommand);
49
this
.Load
+=
new
System.EventHandler(
this
.Page_Load);
50
51
}
52
#endregion
53
54
private
void
MyDataBind()
55
{
56
WebTest.Common.COperator ShowObj
=
new
WebTest.Common.COperator();
57
DataGrid1.DataSource
=
ShowObj.HaveParameter(
"
SearchAllInfoFromTb
"
);
58
DataGrid1.DataBind();
59
}
60
61
public
void
BtEdit(
object
sender, System.Web.UI.WebControls.CommandEventArgs e)
62
{
63
string
Str
=
e.CommandArgument.ToString();
64
//
Response.Write("<script>alert('xx')</script>");
65
Response.Write(Str);
66
}
67
68
public
void
BtDelete(
object
sender, System.Web.UI.WebControls.CommandEventArgs e)
69
{
70
WebTest.Common.COperator DeleteObj
=
new
WebTest.Common.COperator();
71
DeleteObj.NoReturnFunction(e.CommandArgument.ToString(),
"
@StrID
"
,
"
TS_DeleteInfoFromTb
"
);
72
MyDataBind();
73
}
74
75
private
void
DataGrid1_EditCommand(
object
source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
76
{
77
DataGrid1.EditItemIndex
=
e.Item.ItemIndex;
78
MyDataBind();
79
}
80
81
private
void
DataGrid1_CancelCommand(
object
source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
82
{
83
DataGrid1.EditItemIndex
=
-
1
;
84
MyDataBind();
85
}
86
87
private
void
DataGrid1_UpdateCommand(
object
source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
88
{
89
TextBox MyTb
=
(TextBox)e.Item.FindControl(
"
TextBox1
"
);
90
Label MyLb
=
(Label)e.Item.FindControl(
"
LbID
"
);
91
//
int IntT = (int)DataGrid1.DataKeyField[e.Item.ItemIndex];
92
WebTest.Common.COperator UpdateObj
=
new
WebTest.Common.COperator();
93
UpdateObj.NoReturnFunction(MyLb.Text,
"
@StrTID
"
, MyTb.Text,
"
@StrTValues
"
,
"
TS_UpdateTb
"
);
94
95
DataGrid1.EditItemIndex
=
-
1
;
96
MyDataBind();
97
}
98
99
public
void
DataGrid1_OnPageIndexChanged(
object
source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
100
{
101
//
设置当前页的索引
102
DataGrid1.CurrentPageIndex
=
e.NewPageIndex;
103
//
重新进行数据绑定
104
MyDataBind();
105
}
106
}
107
}
108

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

html:
1
<%
@ Page language
=
"
c#
"
Codebehind
=
"
Default.aspx.cs
"
AutoEventWireup
=
"
false
"
Inherits
=
"
WebTest._Default
"
%>
2
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
>
3
<
HTML
>
4
<
HEAD
>
5
<
title
>
Default
</
title
>
6
<
meta
content
="Microsoft Visual Studio .NET 7.1"
name
="GENERATOR"
>
7
<
meta
content
="C#"
name
="CODE_LANGUAGE"
>
8
<
meta
content
="JavaScript"
name
="vs_defaultClientScript"
>
9
<
meta
content
="http://schemas.microsoft.com/intellisense/ie5"
name
="vs_targetSchema"
>
10
</
HEAD
>
11
<
body
MS_POSITIONING
="GridLayout"
>
12
<
form
id
="Form1"
method
="post"
runat
="server"
>
13
<
FONT
face
="宋体"
>
14
<
asp:datagrid
id
="DataGrid1"
style
="Z-INDEX: 101; LEFT: 256px; POSITION: absolute; TOP: 88px"
15
runat
="server"
OnPageIndexChanged
="DataGrid1_OnPageIndexChanged"
AutoGenerateColumns
="False"
16
DataKeyField
="TID"
PageSize
="5"
AllowPaging
="True"
>
17
<
Columns
>
18
<
asp:TemplateColumn
HeaderText
="ID"
>
19
<
ItemTemplate
>
20
<
asp:Label
id
=LbID
runat
="server"
text
='<%#
DataBinder.Eval(Container.DataItem, "TID") %
>
'>
21
</
asp:Label
>
22
</
ItemTemplate
>
23
</
asp:TemplateColumn
>
24
<
asp:TemplateColumn
HeaderText
="Values"
>
25
<
ItemTemplate
>
26
<
asp:Label
id
=LbValues
runat
="server"
text
='<%#
DataBinder.Eval(Container.DataItem, "TValues") %
>
'>
27
</
asp:Label
>
28
</
ItemTemplate
>
29
<
EditItemTemplate
>
30
<
asp:TextBox
id
=TextBox1
runat
="server"
Text
='<%#
DataBinder.Eval(Container.DataItem, "TValues") %
>
' Width="64px">
31
</
asp:TextBox
>
32
</
EditItemTemplate
>
33
</
asp:TemplateColumn
>
34
<
asp:EditCommandColumn
ButtonType
="PushButton"
UpdateText
="更新"
HeaderText
="编辑"
CancelText
="取消"
EditText
="编辑"
></
asp:EditCommandColumn
>
35
<
asp:TemplateColumn
HeaderText
="删除"
>
36
<
ItemTemplate
>
37
<
asp:Button
id
="BtDelete"
runat
="server"
Text
="删除"
CommandArgument
= '<%#
DataBinder.Eval(Container.DataItem, "TID") %
>
' OnCommand = "BtDelete">
38
</
asp:Button
>
39
</
ItemTemplate
>
40
</
asp:TemplateColumn
>
41
</
Columns
>
42
<
PagerStyle
HorizontalAlign
="Right"
Mode
="NumericPages"
></
PagerStyle
>
43
</
asp:datagrid
></
FONT
></
form
>
44
</
body
>
45
</
HTML
>
46
precedure:



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

1
ALTER
PROCEDURE
SearchAllInfoFromTb
2
/**/
/*
3
(
4
@parameter1 datatype = default value,
5
@parameter2 datatype OUTPUT
6
)
7
*/
8
AS
9
select
*
from
Tb

2


3

4

5

6

7

8

9

1
ALTER
PROCEDURE
TS_AddValuesIntoTb
2
(
3
@StrTValues
nvarchar
(
50
)
4
)
5
AS
6
insert
into
Tb
7
(TValues)
8
values
9
(
@StrTValues
)
10

2

3

4

5

6

7

8

9

10

1
ALTER
PROCEDURE
TS_DeleteInfoFromTb
2
(
3
@StrID
varchar
(
50
)
4
)
5
AS
6
delete
Tb
7
where
TID
=
@StrID

2

3

4

5

6

7

1
ALTER
PROCEDURE
TS_UpdateTb
2
(
3
@StrTID
nvarchar
(
50
),
4
@StrTValues
nvarchar
(
50
)
5
)
6
AS
7
update
Tb
8
set
TValues
=
@StrTValues
9
where
TID
=
@StrTID
10

2

3

4

5

6

7

8

9

10
