2023年6月21日发(作者:)
C#⼯具类SqlServerHelper,基于ent封装源码: 1 using System; 2 using c; 3 using ; 4 using ; 5 using ; 6 using ; 7 using ent; 8
9 namespace se 10 { 11 ///
17 /// 执⾏数据库⾮查询操作,返回受影响的⾏数
18 ///
19 /// 数据库连接字符串 20 /// 命令的类型 21 /// SqlServer存储过程名称或PL/SQL命令
22 /// 命令参数集合
23 ///
24 public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms) 25 { 26 SqlCommand cmd = new SqlCommand(); 27 using (SqlConnection conn = new SqlConnection(connectionString)) 28 { 29 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms); 30 int val = eNonQuery(); 31 (); 32 return val; 33 } 34 } 35
36 ///
37 /// 执⾏数据库事务⾮查询操作,返回受影响的⾏数
38 ///
39 /// 数据库事务对象
40 /// Command类型
41 /// SqlServer存储过程名称或PL/SQL命令
42 /// 命令参数集合
43 ///
44 public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms) 45 { 46 SqlCommand cmd = new SqlCommand(); 47 PrepareCommand(cmd, tion, trans, cmdType, cmdText, cmdParms); 48 int val = eNonQuery(); 49 (); 50 return val; 51 } 52
53 ///
54 /// 执⾏数据库⾮查询操作,返回受影响的⾏数
55 ///
56 /// SqlServer数据库连接对象
57 /// Command类型
58 /// SqlServer存储过程名称或PL/SQL命令
59 /// 命令参数集合
60 ///
61 public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms) 62 { 63 if (connection == null) 64 throw new ArgumentNullException("当前数据库连接不存在"); 65 SqlCommand cmd = new SqlCommand(); 66 PrepareCommand(cmd, connection, null, cmdType, cmdText, cmdParms); 67 int val = eNonQuery(); 68 (); 69 return val; 70 } 71
72 ///
73 /// 执⾏数据库查询操作,返回SqlDataReader类型的内存结果集
74 ///
75 /// 数据库连接字符串 76 /// 命令的类型 77 /// SqlServer存储过程名称或PL/SQL命令
78 /// 命令参数集合
79 ///
80 public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms) 81 { 82 SqlCommand cmd = new SqlCommand(); 83 SqlConnection conn = new SqlConnection(connectionString); 84 try 85 { 86 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms); 87 SqlDataReader reader = eReader(onnection); 88 (); 89 return reader; 90 } 91 catch 92 { 93 e(); 94 (); 95 throw; 96 } 97 } 98
99 ///
100 /// 执⾏数据库查询操作,返回DataSet类型的结果集
101 ///
102 /// 数据库连接字符串103 /// 命令的类型104 /// SqlServer存储过程名称或PL/SQL命令
105 /// 命令参数集合
106 ///
107 public static DataSet ExecuteDataSet(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)108 {109 SqlCommand cmd = new SqlCommand();110 SqlConnection conn = new SqlConnection(connectionString);111 DataSet ds = null;112 try113 {114 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);115 SqlDataAdapter adapter = new SqlDataAdapter();116 Command = cmd;117 ds = new DataSet();118 (ds);119 ();120 }121 catch122 {123 throw;124 }125 finally126 {127 e();128 ();129 e();130 }131
132 return ds;133 }134
135 ///
136 /// 执⾏数据库查询操作,返回DataTable类型的结果集
137 ///
138 /// 数据库连接字符串139 /// 命令的类型140 /// SqlServer存储过程名称或PL/SQL命令
141 /// 命令参数集合
142 ///
143 public static DataTable ExecuteDataTable(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)144 {145 SqlCommand cmd = new SqlCommand();146 SqlConnection conn = new SqlConnection(connectionString);147 DataTable dt = null;148
149 try150 {151 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);152 SqlDataAdapter adapter = new SqlDataAdapter();153 Command = cmd;154 dt = new DataTable();155 (dt);156 ();157 }158 catch159 {160 throw;161 }162 finally163 {164 e();165 ();166 e();167 }168
169 return dt;170 }171
172 ///
174 ///
175 /// 数据库连接字符串176 /// 命令的类型177 /// SqlServer存储过程名称或PL/SQL命令
178 /// 命令参数集合
179 ///
180 public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)181 {182 SqlCommand cmd = new SqlCommand();183 SqlConnection conn = new SqlConnection(connectionString);184 object result = null;185 try186 {187 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);188 result = eScalar();189 ();190 }191 catch192 {193 throw;194 }195 finally196 {197 e();198 ();199 e();200 }201
202 return result;203 }204
205 ///
206 /// 执⾏数据库事务查询操作,返回结果集中位于第⼀⾏第⼀列的Object类型的值
207 ///
208 /// ⼀个已存在的数据库事务对象
209 /// 命令类型
210 /// SqlServer存储过程名称或PL/SQL命令
211 /// 命令参数集合
212 ///
213 public static object ExecuteScalar(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)214 {215 if (trans == null)216 throw new ArgumentNullException("当前数据库事务不存在");217 SqlConnection conn = tion;218 if (conn == null)219 throw new ArgumentException("当前事务所在的数据库连接不存在");220
221 SqlCommand cmd = new SqlCommand();222 object result = null;223
224 try225 {226 PrepareCommand(cmd, conn, trans, cmdType, cmdText, cmdParms);227 result = eScalar();228 ();229 }230 catch231 {232 throw;233 }234 finally235 {236 e();237 e();238 ();239 e();240 }241
242 return result;243 }244
245 ///
246 /// 执⾏数据库查询操作,返回结果集中位于第⼀⾏第⼀列的Object类型的值
247 ///
248 /// 数据库连接对象
249 /// Command类型
250 /// SqlServer存储过程名称或PL/SQL命令
251 /// 命令参数集合
252 ///
253 public static object ExecuteScalar(SqlConnection conn, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)254 {255 if (conn == null) throw new ArgumentException("当前数据库连接不存在");256 SqlCommand cmd = new SqlCommand();257 object result = null;258
259 try260 {261 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);262 result = eScalar();263 ();264 }265 catch266 {267 throw;268 }269 finally270 {271 e();272 ();273 e();274 }275
276 return result;277 }278
279 ///
283 /// 存储过程名284 /// 存储过程参数285 ///
295
296 ///
297 /// 执⾏数据库命令前的准备⼯作
298 ///
299 /// Command对象
300 /// 数据库连接对象
301 /// 事务对象
302 /// Command类型
303 /// SqlServer存储过程名称或PL/SQL命令
304 /// 命令参数集合
305 private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)306 {307 if ( != )308 ();309
310 tion = conn;311 dText = cmdText;312
313 if (trans != null)314 ction = trans;315
316 dType = cmdType;317
318 if (cmdParms != null)319 {320 foreach (SqlParameter parm in cmdParms)321 (parm);322 }323 }324
325 ///
2023年6月21日发(作者:)
C#⼯具类SqlServerHelper,基于ent封装源码: 1 using System; 2 using c; 3 using ; 4 using ; 5 using ; 6 using ; 7 using ent; 8
9 namespace se 10 { 11 ///
17 /// 执⾏数据库⾮查询操作,返回受影响的⾏数
18 ///
19 /// 数据库连接字符串 20 /// 命令的类型 21 /// SqlServer存储过程名称或PL/SQL命令
22 /// 命令参数集合
23 ///
24 public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms) 25 { 26 SqlCommand cmd = new SqlCommand(); 27 using (SqlConnection conn = new SqlConnection(connectionString)) 28 { 29 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms); 30 int val = eNonQuery(); 31 (); 32 return val; 33 } 34 } 35
36 ///
37 /// 执⾏数据库事务⾮查询操作,返回受影响的⾏数
38 ///
39 /// 数据库事务对象
40 /// Command类型
41 /// SqlServer存储过程名称或PL/SQL命令
42 /// 命令参数集合
43 ///
44 public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms) 45 { 46 SqlCommand cmd = new SqlCommand(); 47 PrepareCommand(cmd, tion, trans, cmdType, cmdText, cmdParms); 48 int val = eNonQuery(); 49 (); 50 return val; 51 } 52
53 ///
54 /// 执⾏数据库⾮查询操作,返回受影响的⾏数
55 ///
56 /// SqlServer数据库连接对象
57 /// Command类型
58 /// SqlServer存储过程名称或PL/SQL命令
59 /// 命令参数集合
60 ///
61 public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms) 62 { 63 if (connection == null) 64 throw new ArgumentNullException("当前数据库连接不存在"); 65 SqlCommand cmd = new SqlCommand(); 66 PrepareCommand(cmd, connection, null, cmdType, cmdText, cmdParms); 67 int val = eNonQuery(); 68 (); 69 return val; 70 } 71
72 ///
73 /// 执⾏数据库查询操作,返回SqlDataReader类型的内存结果集
74 ///
75 /// 数据库连接字符串 76 /// 命令的类型 77 /// SqlServer存储过程名称或PL/SQL命令
78 /// 命令参数集合
79 ///
80 public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms) 81 { 82 SqlCommand cmd = new SqlCommand(); 83 SqlConnection conn = new SqlConnection(connectionString); 84 try 85 { 86 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms); 87 SqlDataReader reader = eReader(onnection); 88 (); 89 return reader; 90 } 91 catch 92 { 93 e(); 94 (); 95 throw; 96 } 97 } 98
99 ///
100 /// 执⾏数据库查询操作,返回DataSet类型的结果集
101 ///
102 /// 数据库连接字符串103 /// 命令的类型104 /// SqlServer存储过程名称或PL/SQL命令
105 /// 命令参数集合
106 ///
107 public static DataSet ExecuteDataSet(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)108 {109 SqlCommand cmd = new SqlCommand();110 SqlConnection conn = new SqlConnection(connectionString);111 DataSet ds = null;112 try113 {114 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);115 SqlDataAdapter adapter = new SqlDataAdapter();116 Command = cmd;117 ds = new DataSet();118 (ds);119 ();120 }121 catch122 {123 throw;124 }125 finally126 {127 e();128 ();129 e();130 }131
132 return ds;133 }134
135 ///
136 /// 执⾏数据库查询操作,返回DataTable类型的结果集
137 ///
138 /// 数据库连接字符串139 /// 命令的类型140 /// SqlServer存储过程名称或PL/SQL命令
141 /// 命令参数集合
142 ///
143 public static DataTable ExecuteDataTable(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)144 {145 SqlCommand cmd = new SqlCommand();146 SqlConnection conn = new SqlConnection(connectionString);147 DataTable dt = null;148
149 try150 {151 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);152 SqlDataAdapter adapter = new SqlDataAdapter();153 Command = cmd;154 dt = new DataTable();155 (dt);156 ();157 }158 catch159 {160 throw;161 }162 finally163 {164 e();165 ();166 e();167 }168
169 return dt;170 }171
172 ///
174 ///
175 /// 数据库连接字符串176 /// 命令的类型177 /// SqlServer存储过程名称或PL/SQL命令
178 /// 命令参数集合
179 ///
180 public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)181 {182 SqlCommand cmd = new SqlCommand();183 SqlConnection conn = new SqlConnection(connectionString);184 object result = null;185 try186 {187 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);188 result = eScalar();189 ();190 }191 catch192 {193 throw;194 }195 finally196 {197 e();198 ();199 e();200 }201
202 return result;203 }204
205 ///
206 /// 执⾏数据库事务查询操作,返回结果集中位于第⼀⾏第⼀列的Object类型的值
207 ///
208 /// ⼀个已存在的数据库事务对象
209 /// 命令类型
210 /// SqlServer存储过程名称或PL/SQL命令
211 /// 命令参数集合
212 ///
213 public static object ExecuteScalar(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)214 {215 if (trans == null)216 throw new ArgumentNullException("当前数据库事务不存在");217 SqlConnection conn = tion;218 if (conn == null)219 throw new ArgumentException("当前事务所在的数据库连接不存在");220
221 SqlCommand cmd = new SqlCommand();222 object result = null;223
224 try225 {226 PrepareCommand(cmd, conn, trans, cmdType, cmdText, cmdParms);227 result = eScalar();228 ();229 }230 catch231 {232 throw;233 }234 finally235 {236 e();237 e();238 ();239 e();240 }241
242 return result;243 }244
245 ///
246 /// 执⾏数据库查询操作,返回结果集中位于第⼀⾏第⼀列的Object类型的值
247 ///
248 /// 数据库连接对象
249 /// Command类型
250 /// SqlServer存储过程名称或PL/SQL命令
251 /// 命令参数集合
252 ///
253 public static object ExecuteScalar(SqlConnection conn, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)254 {255 if (conn == null) throw new ArgumentException("当前数据库连接不存在");256 SqlCommand cmd = new SqlCommand();257 object result = null;258
259 try260 {261 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);262 result = eScalar();263 ();264 }265 catch266 {267 throw;268 }269 finally270 {271 e();272 ();273 e();274 }275
276 return result;277 }278
279 ///
283 /// 存储过程名284 /// 存储过程参数285 ///
295
296 ///
297 /// 执⾏数据库命令前的准备⼯作
298 ///
299 /// Command对象
300 /// 数据库连接对象
301 /// 事务对象
302 /// Command类型
303 /// SqlServer存储过程名称或PL/SQL命令
304 /// 命令参数集合
305 private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)306 {307 if ( != )308 ();309
310 tion = conn;311 dText = cmdText;312
313 if (trans != null)314 ction = trans;315
316 dType = cmdType;317
318 if (cmdParms != null)319 {320 foreach (SqlParameter parm in cmdParms)321 (parm);322 }323 }324
325 ///
发布评论