2023年6月21日发(作者:)

SqlHelper带详细中⽂注释 1 using System;

2 using c;

3 using ;

4 using ;

5 //对数据库进⾏操作引⼊命名空间

6 using ;

7 using ent;

8 using uration;

9

10

11 namespace DAL

12 {

13 public class SQLhelper

14 {

15

16

17 /// 变量定义

18 ///

19 private SqlConnection conn = null;

20 private SqlCommand cmd = null;

21 private SqlDataReader sdr = null;

22

23 /// SQLhelper connStr 字符串的连接

24 ///

25 04 011

26 public SQLhelper()

27 {

28 string connStr = tionStrings["connStr"].ConnectionString; //连接SQL数据表 29 conn = new SqlConnection(connStr); //连接通道 30

31 }

32

33

34

35 /// 获取conn

36 ///

37 /// 为什么要这样写有什么意义呢?

38 private SqlConnection GetConn()

39 {

40 if ( == ) //-判断当前连接的状态 如果当前的连接是关闭状态的话 结果为True

41 {

42 (); //-打开数据库 43 }

44

45 return conn; //返回结果 46 }

47

48

49

50

51 /// 该⽅法传⼊⼀个增删改SQL语句或是存储过程

52 ///

53 /// 要执⾏的增删改SQL语句或是存储过程

54 /// 返回更新的记录数

55 public int ExecuteNonQuery(string cmdText,CommandType ct)

56 {

57 //-执⾏⾮查询 ⽐如说 insert Updata Delect

58 //不定义的时候返回的初值是零

59 int res;

60 try

61 {

62 //-1、将预见可能引发异常的代码包含在try语句块中。

63 //-2、如果发⽣了异常,则转⼊catch的执⾏。 64 cmd = new SqlCommand(cmdText, GetConn());

65

66 dType = ct;

67 res = eNonQuery();

68 }

69 catch (Exception ex)

70 {

71 //-这将捕获任何发⽣的异常。另外,还提供e参数,你可以在处理异常时使⽤e参数来获得有关异常的信息。 72 throw ex;

73 }

74 finally

75 {

76 //-不管什么情况都会执⾏,包括try catch ⾥⾯⽤了return ,可以理解为只要执⾏了try或者catch,就⼀定会执⾏ finally

77 //-清理⼯作。如关闭数据库连接。 78 if ( == )

79 {

80 (); 81 }

82

83 }

84 return res; //-返回给调⽤者 85 }

86

87

88 /// 执⾏带参数的增删改语句或是存储过程

89 /// 执⾏带参数的增删改语句或是存储过程

90 ///

91 /// 带参数的SQL语句或是存储过程

92 /// 参数集合

93 ///

94

95 public int ExecuteNonQuery(string cmdText, SqlParameter[] paras,CommandType ct)

96 {

97 int res;

98 using (cmd = new SqlCommand(cmdText, GetConn()))

99 {

100 dType = ct;

101 ge(paras); //将每个查询出的数 添加到数组102

103 res = eNonQuery();

104 }

105

106 return res;

107

108 }

109

110

111 /// 该⽅法传⼊⼀个查询SQL语句或是存储过程

112 /// 该⽅法传⼊⼀个查询SQL语句或是存储过程

113 ///

114 /// SQL查询语句或存储过程

115 ///

116 public DataTable ExecuteQuery(string cmtText,CommandType ct)

117 {

118 DataTable dt = new DataTable();

119 cmd = new SqlCommand(cmtText, GetConn());

120 dType = ct;

121 //关系sdr的同是将会关闭conn

122 using (sdr = eReader(onnection))

123 {

124 (sdr);

125 }

126 return dt;

127 }

128

129

130 /// 执⾏带参数的SQL语句或是存储过程

131 /// 执⾏带参数的SQL语句或是存储过程

132 ///

133 /// SQL语句或是存储过程

134 ///

135 ///

136 public DataTable ExecuteQuery(string cmdText, SqlParameter[] paras,CommandType ct)

137 {

138 DataTable dt = new DataTable();

139

140 cmd = new SqlCommand(cmdText, GetConn());

141 dType = ct;

142 ge(paras);

143 //关系sdr的同是将会关闭conn

144 using (sdr = eReader(onnection))

145 {

146 (sdr);

147 }

148 return dt;

149 }

150

151 }

152

153 }

2023年6月21日发(作者:)

SqlHelper带详细中⽂注释 1 using System;

2 using c;

3 using ;

4 using ;

5 //对数据库进⾏操作引⼊命名空间

6 using ;

7 using ent;

8 using uration;

9

10

11 namespace DAL

12 {

13 public class SQLhelper

14 {

15

16

17 /// 变量定义

18 ///

19 private SqlConnection conn = null;

20 private SqlCommand cmd = null;

21 private SqlDataReader sdr = null;

22

23 /// SQLhelper connStr 字符串的连接

24 ///

25 04 011

26 public SQLhelper()

27 {

28 string connStr = tionStrings["connStr"].ConnectionString; //连接SQL数据表 29 conn = new SqlConnection(connStr); //连接通道 30

31 }

32

33

34

35 /// 获取conn

36 ///

37 /// 为什么要这样写有什么意义呢?

38 private SqlConnection GetConn()

39 {

40 if ( == ) //-判断当前连接的状态 如果当前的连接是关闭状态的话 结果为True

41 {

42 (); //-打开数据库 43 }

44

45 return conn; //返回结果 46 }

47

48

49

50

51 /// 该⽅法传⼊⼀个增删改SQL语句或是存储过程

52 ///

53 /// 要执⾏的增删改SQL语句或是存储过程

54 /// 返回更新的记录数

55 public int ExecuteNonQuery(string cmdText,CommandType ct)

56 {

57 //-执⾏⾮查询 ⽐如说 insert Updata Delect

58 //不定义的时候返回的初值是零

59 int res;

60 try

61 {

62 //-1、将预见可能引发异常的代码包含在try语句块中。

63 //-2、如果发⽣了异常,则转⼊catch的执⾏。 64 cmd = new SqlCommand(cmdText, GetConn());

65

66 dType = ct;

67 res = eNonQuery();

68 }

69 catch (Exception ex)

70 {

71 //-这将捕获任何发⽣的异常。另外,还提供e参数,你可以在处理异常时使⽤e参数来获得有关异常的信息。 72 throw ex;

73 }

74 finally

75 {

76 //-不管什么情况都会执⾏,包括try catch ⾥⾯⽤了return ,可以理解为只要执⾏了try或者catch,就⼀定会执⾏ finally

77 //-清理⼯作。如关闭数据库连接。 78 if ( == )

79 {

80 (); 81 }

82

83 }

84 return res; //-返回给调⽤者 85 }

86

87

88 /// 执⾏带参数的增删改语句或是存储过程

89 /// 执⾏带参数的增删改语句或是存储过程

90 ///

91 /// 带参数的SQL语句或是存储过程

92 /// 参数集合

93 ///

94

95 public int ExecuteNonQuery(string cmdText, SqlParameter[] paras,CommandType ct)

96 {

97 int res;

98 using (cmd = new SqlCommand(cmdText, GetConn()))

99 {

100 dType = ct;

101 ge(paras); //将每个查询出的数 添加到数组102

103 res = eNonQuery();

104 }

105

106 return res;

107

108 }

109

110

111 /// 该⽅法传⼊⼀个查询SQL语句或是存储过程

112 /// 该⽅法传⼊⼀个查询SQL语句或是存储过程

113 ///

114 /// SQL查询语句或存储过程

115 ///

116 public DataTable ExecuteQuery(string cmtText,CommandType ct)

117 {

118 DataTable dt = new DataTable();

119 cmd = new SqlCommand(cmtText, GetConn());

120 dType = ct;

121 //关系sdr的同是将会关闭conn

122 using (sdr = eReader(onnection))

123 {

124 (sdr);

125 }

126 return dt;

127 }

128

129

130 /// 执⾏带参数的SQL语句或是存储过程

131 /// 执⾏带参数的SQL语句或是存储过程

132 ///

133 /// SQL语句或是存储过程

134 ///

135 ///

136 public DataTable ExecuteQuery(string cmdText, SqlParameter[] paras,CommandType ct)

137 {

138 DataTable dt = new DataTable();

139

140 cmd = new SqlCommand(cmdText, GetConn());

141 dType = ct;

142 ge(paras);

143 //关系sdr的同是将会关闭conn

144 using (sdr = eReader(onnection))

145 {

146 (sdr);

147 }

148 return dt;

149 }

150

151 }

152

153 }