Ajax的注册小示例




1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 using System.Data.SqlClient;
11
12 /// <summary>
13 /// Connection 的摘要说明
14 /// </summary>
15 public class Connection
16 {
17 public Connection()
18 {
19 //
20 // TODO: 在此处添加构造函数逻辑
21 //
22 }
23
24 public static SqlConnection getConnection()
25 {
26 return new SqlConnection (System .Configuration .ConfigurationManager .ConnectionStrings ["ConnectionString"].ConnectionString );
27 }
28 }
29


1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 using System.Data.SqlClient;
11
12 /// <summary>
13 /// User 的摘要说明
14 /// </summary>
15 public class User
16 {
17 public User()
18 {
19 //
20 // TODO: 在此处添加构造函数逻辑
21 //
22 }
23
24 /// <summary>
25 /// 检测用户是否存在
26 /// </summary>
27 /// <param name="userName">用户名</param>
28 /// <returns>bool</returns>
29 public bool checkName(string userName)
30 {
31 SqlConnection userConnection = Connection.getConnection();
32 SqlCommand userCommand = new SqlCommand("checkName", userConnection);
33
34 userCommand.CommandType = CommandType.StoredProcedure;
35 userCommand.Parameters.Add("@userName", SqlDbType.VarChar, 50);
36 userCommand.Parameters["@userName"].Value = userName;
37 userCommand.Connection.Open();
38 int n = (int)userCommand.ExecuteScalar();
39 userCommand.Connection.Close();
40
41 if (n > 0)
42 {
43 return true;
44 }
45 else
46 {
47 return false;
48 }
49 }
50
51 /// <summary>
52 /// 插入用户
53 /// </summary>
54 /// <param name="userName">用户名</param>
55 /// <param name="userPwd">密码</param>
56 /// <returns></returns>
57 public bool insertUserInfo(string userName, string userPwd)
58 {
59 SqlConnection userConnection = Connection.getConnection();
60 SqlCommand userCommand = new SqlCommand("insertUserInfo", userConnection);
61
62 userCommand.CommandType = CommandType.StoredProcedure;
63 userCommand.Parameters.Add("@userName", SqlDbType.VarChar, 50);
64 userCommand.Parameters["@userName"].Value = userName;
65
66 userCommand.Parameters.Add("@userPwd", SqlDbType.VarChar, 50);
67 userCommand.Parameters["@userPwd"].Value = userPwd;
68
69 userCommand.Connection.Open();
70 userCommand.ExecuteNonQuery();
71
72 userCommand.Connection.Close();
73 return true;
74 }
75
76 /// <summary>
77 /// 查询用户信息
78 /// </summary>
79 /// <returns>SqlDataReader</returns>
80 public SqlDataReader selectUserInfo()
81 {
82 SqlConnection userConnection = Connection.getConnection();
83 SqlCommand userCommand = new SqlCommand("select * from UserInfo",userConnection );
84 userCommand.Connection.Open() ;
85 return userCommand.ExecuteReader();
86
87 }
88
89 /// <summary>
90 /// 删除用户
91 /// </summary>
92 /// <param name="userName">用户名</param>
93 /// <returns>bool</returns>
94 public bool deleteUser(string userName)
95 {
96 SqlConnection userConnection = Connection.getConnection();
97 SqlCommand userCommand = new SqlCommand("deleteUser", userConnection);
98 userCommand.CommandType = CommandType.StoredProcedure;
99
100 userCommand.Parameters.Add("@userName", SqlDbType.VarChar, 50);
101 userCommand.Parameters["@userName"].Value = userName;
102
103 userCommand.Connection.Open();
104
105 userCommand.ExecuteNonQuery();
106 userCommand.Connection.Close();
107
108 return true;
109 }
110
111 /// <summary>
112 /// 更新用户信息
113 /// </summary>
114 /// <param name="userName"></param>
115 /// <param name="userPwd"></param>
116 /// <returns></returns>
117 public bool updateUserInfo(string userName, string userPwd)
118 {
119 SqlConnection userConnection = Connection.getConnection();
120 SqlCommand userCommand = new SqlCommand("updateUserInfo", userConnection);
121 userCommand.CommandType = CommandType.StoredProcedure;
122 userCommand.Parameters.Add("@userName", SqlDbType.VarChar, 50);
123 userCommand.Parameters["@userName"].Value = userName;
124 userCommand.Parameters.Add("@userPwd", SqlDbType.VarChar, 50);
125 userCommand.Parameters["@userPwd"].Value = userPwd;
126
127 userCommand.Connection.Open();
128 userCommand.ExecuteNonQuery();
129 userCommand.Connection.Close();
130
131 return true;
132 }
133 }
134


1



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

1

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



1

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

1
set ANSI_NULLS ON
2
set QUOTED_IDENTIFIER ON
3
go
4![]()
5
ALTER PROCEDURE [dbo].[checkName]
6
@userName varchar(50)
7
AS
8
SELECT COUNT(*) FROM UserInfo
9
WHERE UserName=@userName
10![]()

2

3

4

5

6

7

8

9

10

1
set ANSI_NULLS ON
2
set QUOTED_IDENTIFIER ON
3
go
4![]()
5
ALTER PROCEDURE [dbo].[deleteUser]
6
@userName varchar(50)
7
AS
8
DELETE FROM UserInfo WHERE UserName=@userName
9
return
10![]()

2

3

4

5

6

7

8

9

10



1 set ANSI_NULLS ON
2 set QUOTED_IDENTIFIER ON
3 go
4
5 ALTER PROCEDURE [dbo].[insertUserInfo]
6 @userName varchar(50),
7 @userPwd varchar(50)
8 AS
9 INSERT INTO UserInfo VALUES(@userName,@userPwd)
10 return
11
12


1 set ANSI_NULLS ON
2 set QUOTED_IDENTIFIER ON
3 go
4
5 ALTER PROCEDURE [dbo].[updateUserInfo]
6 @userName varchar(50),
7 @userPwd varchar(50)
8 AS
9 UPDATE UserInfo SET UserPwd=@userPwd
10 WHERE UserName=@userName
11 return
12
13