工作中数据层开始的时候我们是用NHIBERNATE的,后来发觉这个学习成本比较高,还有就是业务比较复杂的时候配置等各方面都比较麻烦,而且控制不好的话链接很容易爆满,后来我们该用直接用ADO.NET自己封装了一个操作类来满足这方面的需求,下面的代码是这个HELPER类的雏形。看起来都比较容易明白,所以注析就少了点。
这个类是按照NHIBERNATE的操作习惯进行封装的,该类把常用的基本操作和参数的设置以及事务等做了一次封装,因为时间关系先把这个类拷进来,具体的操作方法下次再给出来,
Code
1using System;
2using System.Collections;
3using System.Data;
4using System.Text;
5using System.Text.RegularExpressions;
6using Oracle.DataAccess;
7using Oracle.DataAccess.Client;
8using System.Data.SqlClient;
9using System.Reflection;
10
11namespace KingCardService.Util
12{
13 public class DataHelper : IDisposable
14 {
15 枚举#region 枚举
16 /**//// <summary>
17 /// 数据库类型
18 /// </summary>
19 public enum MyDbType
20 {
21 ORACLE,
22 MSSQL
23 }
24 #endregion
25
26 字段#region 字段
27 private string _connectionString;
28 private IDbConnection _conn = null;
29 private IDbCommand _comm = null;
30 private IDbTransaction _transaction = null;
31 private CommandType _type = CommandType.Text;
32 private MyDbType _myDbType = MyDbType.MSSQL;
33 private ArrayList _pars = new ArrayList();
34 private ArrayList _trancomms = new ArrayList();
35 #endregion
36 public DataHelper(string connectionString):this(connectionString,MyDbType.ORACLE)
37 {
38 }
39 public DataHelper(string connectionString,MyDbType dbType)
40 {
41 _connectionString = connectionString;
42 _myDbType = dbType;
43 SetConnection();
44 }
45 private void SetConnection()
46 {
47 if (_conn == null)
48 {
49 if (_myDbType == MyDbType.ORACLE)
50 {
51 _conn = new OracleConnection(_connectionString);
52 }
53 else
54 {
55 _conn = new SqlConnection(_connectionString);
56 }
57 }
58 if (_conn.State == ConnectionState.Closed)
59 {
60 _conn.Open();
61 }
62 }
63 属性#region 属性
64 /**//// <summary>
65 /// 数据库类型
66 /// 默认为ORACLE
67 /// 数据库
68 /// </summary>
69 public MyDbType DBType
70 {
71 get { return _myDbType; }
72 set { _myDbType = value; }
73 }
74
75 /**//// <summary>
76 /// 获取数据库连接
77 /// </summary>
78 public IDbConnection Connection
79 {
80 get {
81 return _conn;
82 }
83 }
84 /**//// <summary>
85 /// 事务
86 /// </summary>
87 public IDbTransaction Transaction
88 {
89 get { return _transaction; }
90 }
91 #endregion
92
93 事务处理#region 事务处理
94 /**//// <summary>
95 /// 事务开始
96 /// </summary>
97 public void BeginTransaction()
98 {
99 if (_conn != null)
100 {
101 if (_conn.State == ConnectionState.Closed)
102 _conn.Open();
103 _transaction = _conn.BeginTransaction();
104 }
105 else
106 {
107 throw new Exception("未设置连接!");
108 }
109 }
110
111 /**//// <summary>
112 /// 事务提交
113 /// </summary>
114 public void CommitTransaction()
115 {
116 _transaction.Commit();
117 _trancomms.Clear();
118 }
119
120 /**//// <summary>
121 /// 事务回滚
122 /// </summary>
123 public void RollbackTransaction()
124 {
125 _transaction.Rollback();
126 _trancomms.Clear();
127 }
128 /**//// <summary>
129 /// IDbCommand 对象的克隆
130 /// </summary>
131 /// <param name="comm"></param>
132 /// <returns></returns>
133 private IDbCommand DeepClone(IDbCommand comm)
134 {
135 if (comm != null)
136 {
137 IDbCommand command = comm.Connection.CreateCommand();
138 command.CommandText = comm.CommandText;
139 command.CommandTimeout = comm.CommandTimeout;
140 command.CommandType = comm.CommandType;
141 foreach (IDataParameter p in comm.Parameters)
142 {
143 IDataParameter p2 = command.CreateParameter();
p2.ParameterName = p.ParameterName;
p2.DbType = p.DbType;
p2.Value = p.Value;
command.Parameters.Add(p2);
144 }
145 return command;
146 }
147 else
148 {
149 return null;
150 }
151 }
152 #endregion
153
154 创建查询#region 创建查询
155 /**//// <summary>
156 /// CreateQuery(string sql)
157 /// </summary>
158 /// <param name="sql">string sql</param>
159 /// <returns>DataHelper</returns>
160 public DataHelper CreateQuery(string sql)
161 {
162 if (_conn == null)
163 throw new Exception("请先设置数据库连接!");
164 _comm = _conn.CreateCommand();
165 _comm.CommandText = sql;
166 if(_conn.State == ConnectionState.Closed)
167 _conn.Open();
168 return this;
169 }
170
171 /**//// <summary>
172 /// CreateQuery(IDbConnection conn, string sql, CommandType commandType)
173 /// </summary>
174 /// <param name="conn">IDbConnection conn</param>
175 /// <param name="sql">string sql</param>
176 /// <param name="commandType">CommandType commandType</param>
177 /// <returns>DataHelper</returns>
178 public DataHelper CreateQuery(string sql, CommandType commandType)
179 {
180 _type = commandType;
181 return CreateQuery(sql);
182 }
183 #endregion
184
185 执行查询#region 执行查询
186 /**//// <summary>
187 /// 执行事务操作查询
188 /// </summary>
189 /// <returns>int val</returns>
190 public int ExecuteTransactionNoneQuery()
191 {
192 if (_transaction != null)
193 {
194 AddParameterToCommand();
195 IDbCommand comm = DeepClone(_comm);
196 _trancomms.Add(comm);
197 _comm.Dispose();
198 _comm = null;
199 comm.Transaction = _transaction;
200 return comm.ExecuteNonQuery();
201 }
202 else
203 {
204 throw new Exception("事务未打开!");
205 }
206 }
207
208 /**//// <summary>
209 /// ExecuteNonQuery()
210 /// Exceptions:
211 /// System.InvalidOperationException
212 /// </summary>
213 /// <returns>int</returns>
214 public int ExecuteNonQuery()
215 {
216 AddParameterToCommand();
217 try
218 {
219 int result = _comm.ExecuteNonQuery();
220 return result;
221 }
222 catch (InvalidOperationException ex)
223 {
224 if (_conn != null && _conn.State == ConnectionState.Open)
225 _conn.Close();
226 throw ex;
227 }
228 catch (Exception ex)
229 {
230 if (_conn != null && _conn.State == ConnectionState.Open)
231 _conn.Close();
232 throw ex;
233 }
234 }
235
236 /**//// <summary>
237 /// ExecuteScalar()
238 /// </summary>
239 /// <returns>object</returns>
240 public object ExecuteScalar()
241 {
242 AddParameterToCommand();
243 try
244 {
245 object obj = _comm.ExecuteScalar();
246 return obj;
247 }
248 catch (Exception ex)
249 {
250 if(_conn.State == ConnectionState.Open)
251 _conn.Close();
252 throw ex;
253 }
254 }
255
256 /**//// <summary>
257 /// ExecuteReader()
258 /// </summary>
259 /// <returns>IDataReader</returns>
260 public IDataReader ExecuteReader()
261 {
262 AddParameterToCommand();
263 try
264 {
265 return _comm.ExecuteReader();
266 }
267 catch (Exception ex)
268 {
269 if (_conn != null && _conn.State == ConnectionState.Open)
270 _conn.Close();
271 throw ex;
272 }
273 }
274
275 /**//// <summary>
276 /// ExecuteReader(CommandBehavior commandBehavior)
277 /// </summary>
278 /// <returns>IDataReader</returns>
279 public IDataReader ExecuteReader(CommandBehavior commandBehavior)
280 {
281 AddParameterToCommand();
282 try
283 {
284 return _comm.ExecuteReader(commandBehavior);
285 }
286 catch (Exception ex)
287 {
288 if (_conn != null && _conn.State == ConnectionState.Open)
289 _conn.Close();
290 throw ex;
291 }
292 }
293 /**//// <summary>
294 /// ExecuteDataSet(MyDbType myDbType)
295 /// </summary>
296 /// <returns>DataSet</returns>
297 public DataSet ExecuteDataSet()
298 {
299 AddParameterToCommand();
300 if (_conn != null && _conn.State == ConnectionState.Open)
301 _conn.Close();
302 DataSet ds = new DataSet();
303 switch (_myDbType)
304 {
305 case MyDbType.ORACLE:
306 using(OracleDataAdapter adapter = new OracleDataAdapter((OracleCommand)_comm))
307 {
308 adapter.Fill(ds);
309 }
310 break;
311 case MyDbType.MSSQL:
312 using(SqlDataAdapter adapter = new SqlDataAdapter((SqlCommand)_comm))
313 {
314 adapter.Fill(ds);
315 }
316 break;
317 }
318 return ds;
319 }
320
321 /**//// <summary>
322 /// ExecuteDataSet(int startRecord, int maxRecords, string srcTable)
323 /// </summary>
324 /// <param name="startRecord">int startRecord</param>
325 /// <param name="maxRecords">int maxRecords</param>
326 /// <param name="srcTable">string srcTable</param>
327 /// <returns>DataSet</returns>
328 public DataSet ExecuteDataSet(int startRecord, int maxRecords, string srcTable)
329 {
330 AddParameterToCommand();
331 if (_conn != null && _conn.State == ConnectionState.Open)
332 _conn.Close();
333 DataSet ds = new DataSet();
334 switch (_myDbType)
335 {
336 case MyDbType.ORACLE:
337 using (OracleDataAdapter adapter = new OracleDataAdapter((OracleCommand)_comm))
338 {
339 adapter.Fill(ds, startRecord, maxRecords, srcTable);
340 }
341 break;
342 case MyDbType.MSSQL:
343 using (SqlDataAdapter adapter = new SqlDataAdapter((SqlCommand)_comm))
344 {
345 adapter.Fill(ds, startRecord, maxRecords, srcTable);
346 }
347 break;
348 }
349 return ds;
350 }
351
352 #endregion
353
354 设置参数#region 设置参数
355 /**//// <summary>
356 /// 将设置好的参数加到Command中去
357 /// </summary>
358 private void AddParameterToCommand()
359 {
360 _comm.Parameters.Clear();
361 if(_type == CommandType.StoredProcedure)
362 {
363 foreach(IDataParameter p in _pars)
364 {
365 _comm.Parameters.Add(p);
366 }
367 }
368 else
369 {
370 string sql = _comm.CommandText;
371 string spliter = _myDbType == MyDbType.MSSQL ? "@" : ":";
372 string pattern = string.Format(@"{0}\w+", spliter);
373 System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(pattern, RegexOptions.IgnoreCase);
374 MatchCollection col = reg.Matches(sql);
375 foreach(Match item in col)
376 {
377 string val = item.Value.ToLower();
378 foreach(IDataParameter p in _pars)
379 {
380 if(p.ParameterName.ToLower() == val && !_comm.Parameters.Contains(val))
381 {
382 _comm.Parameters.Add(p);
383 break;
384 }
385 }
386 }
387 }
388 _pars.Clear();
389 }
390
391 /**//// <summary>
392 /// 设置参数
393 /// </summary>
394 /// <param name="name"></param>
395 /// <param name="dbType"></param>
396 /// <param name="value"></param>
397 /// <returns></returns>
398 private IDataParameter AddParameter(string name, DbType dbType, object value)
399 {
400 if (_comm != null)
401 {
402 IDataParameter p = _comm.CreateParameter();
403 p.ParameterName = _myDbType.Equals(MyDbType.MSSQL) ? string.Format("@{0}", name) : string.Format(":{0}", name);
404 p.DbType = dbType;
405 p.Value = value;
406 _pars.Add(p);
407 return p;
408 }
409 else
410 {
411 throw new Exception("IDbCommand对象为空!");
412 }
413 }
414
415 /**//// <summary>
416 /// 设置参数
417 /// </summary>
418 /// <param name="name"></param>
419 /// <param name="dbType"></param>
420 /// <param name="value"></param>
421 /// <returns></returns>
422 private IDataParameter AddParameter(string name, DbType dbType, object value, ParameterDirection parameterDirection)
423 {
424 IDataParameter p = AddParameter(name, dbType, value);
425 if(_type == CommandType.StoredProcedure)
426 {
427 p.Direction = parameterDirection;
428 }
429 return p;
430 }
431
432 /**//// <summary>
433 /// SetString(string name, string val)
434 /// </summary>
435 /// <param name="name">string name</param>
436 /// <param name="val">string val</param>
437 /// <returns>DataHelper</returns>
438 public DataHelper SetString(string name, string val)
439 {
440 AddParameter(name, DbType.String, (val == null ? string.Empty : val));
441 return this;
442 }
443
444 /**//// <summary>
445 /// SetString(string name, string val, ParameterDirection parameterDirection)
446 /// </summary>
447 /// <param name="name">string name</param>
448 /// <param name="val">string val</param>
449 /// <param name="parameterDirection">ParameterDirection parameterDirection</param>
450 /// <returns>DataHelper</returns>
451 public DataHelper SetString(string name, string val, ParameterDirection parameterDirection)
452 {
453 AddParameter(name, DbType.String, val, parameterDirection);
454 return this;
455 }
456
457 public DataHelper SetDouble(string name, double val)
458 {
459 AddParameter(name, DbType.Double, val);
460 return this;
461 }
462
463 public DataHelper SetDouble(string name, double val, ParameterDirection parameterDirection)
464 {
465 AddParameter(name, DbType.Double, parameterDirection);
466 return this;
467 }
468
469 public DataHelper SetDecimal(string name, decimal val)
470 {
471 AddParameter(name, DbType.Decimal, val);
472 return this;
473 }
474
475 public DataHelper SetDecimal(string name, decimal val, ParameterDirection parameterDirection)
476 {
477 AddParameter(name, DbType.Decimal, val, parameterDirection);
478 return this;
479 }
480
481 /**//// <summary>
482 /// SetInt32(string name, Int32 val)
483 /// </summary>
484 /// <param name="name">string name</param>
485 /// <param name="val">Int32 val</param>
486 /// <returns>DataHelper</returns>
487 public DataHelper SetInt32(string name, Int32 val)
488 {
489 AddParameter(name, DbType.Int32, val);
490 return this;
491 }
492
493 /**//// <summary>
494 /// SetInt32(string name, Int32 val, ParameterDirection parameterDirection)
495 /// </summary>
496 /// <param name="name">string name</param>
497 /// <param name="val">Int32 va</param>
498 /// <param name="parameterDirection">ParameterDirection parameterDirection</param>
499 /// <returns>DataHelper</returns>
500 public DataHelper SetInt32(string name, Int32 val, ParameterDirection parameterDirection)
501 {
502 AddParameter(name, DbType.Int32, val, parameterDirection);
503 return this;
504 }
505
506 /**//// <summary>
507 /// SetInt16(string name, Int16 val)
508 /// </summary>
509 /// <param name="name">string name</param>
510 /// <param name="val">Int16 val</param>
511 /// <returns>DataHelper</returns>
512 public DataHelper SetInt16(string name, Int16 val)
513 {
514 AddParameter(name, DbType.Int16, val);
515 return this;
516 }
517
518 /**//// <summary>
519 /// SetInt16(string name, Int16 val, ParameterDirection parameterDirection)
520 /// </summary>
521 /// <param name="name">string name</param>
522 /// <param name="val">Int16 val</param>
523 /// <param name="parameterDirection">ParameterDirection parameterDirection</param>
524 /// <returns>DataHelper</returns>
525 public DataHelper SetInt16(string name, Int16 val, ParameterDirection parameterDirection)
526 {
527 AddParameter(name, DbType.Int16, val, parameterDirection);
528 return this;
529 }
530
531 /**//// <summary>
532 /// SetDate(string name, DateTime val)
533 /// </summary>
534 /// <param name="name">string name</param>
535 /// <param name="val">DateTime val</param>
536 /// <returns>DataHelper</returns>
537 public DataHelper SetDate(string name, DateTime val)
538 {
539 AddParameter(name, DbType.Date, val);
540 return this;
541 }
542
543 /**//// <summary>
544 /// SetDate(string name, DateTime val, ParameterDirection parameterDirection)
545 /// </summary>
546 /// <param name="name">string name</param>
547 /// <param name="val">DateTime val</param>
548 /// <param name="parameterDirection">ParameterDirection parameterDirection</param>
549 /// <returns>DataHelper</returns>
550 public DataHelper SetDate(string name, DateTime val, ParameterDirection parameterDirection)
551 {
552 AddParameter(name, DbType.Date, val, parameterDirection);
553 return this;
554 }
555
556 /**//// <summary>
557 /// SetDateTime(string name, DateTime val)
558 /// </summary>
559 /// <param name="name">string name</param>
560 /// <param name="val">DateTime val</param>
561 /// <returns>DataHelper</returns>
562 public DataHelper SetDateTime(string name, DateTime val)
563 {
564 AddParameter(name, DbType.DateTime, val);
565 return this;
566 }
567
568 /**//// <summary>
569 /// SetDateTime(string name, DateTime val, ParameterDirection parameterDirection)
570 /// </summary>
571 /// <param name="name">string name</param>
572 /// <param name="val">DateTime val</param>
573 /// <param name="parameterDirection">ParameterDirection parameterDirection</param>
574 /// <returns>DataHelper</returns>
575 public DataHelper SetDateTime(string name, DateTime val, ParameterDirection parameterDirection)
576 {
577 AddParameter(name, DbType.DateTime, val, parameterDirection);
578 return this;
579 }
580
581 /**//// <summary>
582 /// SetEnum(string name, Enum val)
583 /// </summary>
584 /// <param name="name">string name</param>
585 /// <param name="val">Enum val</param>
586 /// <returns>DataHelper</returns>
587 public DataHelper SetEnum(string name, Enum val)
588 {
589 AddParameter(name, DbType.Int32, val.GetHashCode());
590 return this;
591 }
592
593 /**//// <summary>
594 /// SetEnum(string name, Enum val, ParameterDirection parameterDirection)
595 /// </summary>
596 /// <param name="name">string name</param>
597 /// <param name="val">Enum val</param>
598 /// <param name="parameterDirection">ParameterDirection parameterDirection</param>
599 /// <returns>DataHelper</returns>
600 public DataHelper SetEnum(string name, Enum val, ParameterDirection parameterDirection)
601 {
602 AddParameter(name, DbType.Int32, val.GetHashCode(), parameterDirection);
603 return this;
604 }
605
606 /**//// <summary>
607 /// SetEnum(string name, bool val)
608 /// </summary>
609 /// <param name="name">string name</param>
610 /// <param name="val">bool val</param>
611 /// <returns>DataHelper</returns>
612 public DataHelper SetBoolean(string name, bool val)
613 {
614 AddParameter(name, DbType.Byte, (val ? 1 : 0));
615 return this;
616 }
617
618 /**//// <summary>
619 /// SetEnum(string name, bool val, ParameterDirection parameterDirection)
620 /// </summary>
621 /// <param name="name">string name</param>
622 /// <param name="val">bool val</param>
623 /// <param name="parameterDirection">ParameterDirection parameterDirection</param>
624 /// <returns>DataHelper</returns>
625 public DataHelper SetBoolean(string name, bool val, ParameterDirection parameterDirection)
626 {
627 AddParameter(name, DbType.Byte, (val ? 1 : 0), parameterDirection);
628 return this;
629 }
630 #endregion
631
632 IDisposable 成员#region IDisposable 成员
633
634 public void Dispose()
635 {
636 if(_comm != null)
637 _comm.Dispose();
638 if (_trancomms != null)
639 {
640 for (int i = 0; i < _trancomms.Count; i++)
641 {
642 if((IDbCommand)_trancomms[i] != null)
643 ((IDbCommand)_trancomms[i]).Dispose();
644 }
645 }
646
647 if(_conn != null)
648 _conn.Dispose();
649 }
650
651 #endregion
652 }
653}
654
655
1using System;
2using System.Collections;
3using System.Data;
4using System.Text;
5using System.Text.RegularExpressions;
6using Oracle.DataAccess;
7using Oracle.DataAccess.Client;
8using System.Data.SqlClient;
9using System.Reflection;
10
11namespace KingCardService.Util
12{
13 public class DataHelper : IDisposable
14 {
15 枚举#region 枚举
16 /**//// <summary>
17 /// 数据库类型
18 /// </summary>
19 public enum MyDbType
20 {
21 ORACLE,
22 MSSQL
23 }
24 #endregion
25
26 字段#region 字段
27 private string _connectionString;
28 private IDbConnection _conn = null;
29 private IDbCommand _comm = null;
30 private IDbTransaction _transaction = null;
31 private CommandType _type = CommandType.Text;
32 private MyDbType _myDbType = MyDbType.MSSQL;
33 private ArrayList _pars = new ArrayList();
34 private ArrayList _trancomms = new ArrayList();
35 #endregion
36 public DataHelper(string connectionString):this(connectionString,MyDbType.ORACLE)
37 {
38 }
39 public DataHelper(string connectionString,MyDbType dbType)
40 {
41 _connectionString = connectionString;
42 _myDbType = dbType;
43 SetConnection();
44 }
45 private void SetConnection()
46 {
47 if (_conn == null)
48 {
49 if (_myDbType == MyDbType.ORACLE)
50 {
51 _conn = new OracleConnection(_connectionString);
52 }
53 else
54 {
55 _conn = new SqlConnection(_connectionString);
56 }
57 }
58 if (_conn.State == ConnectionState.Closed)
59 {
60 _conn.Open();
61 }
62 }
63 属性#region 属性
64 /**//// <summary>
65 /// 数据库类型
66 /// 默认为ORACLE
67 /// 数据库
68 /// </summary>
69 public MyDbType DBType
70 {
71 get { return _myDbType; }
72 set { _myDbType = value; }
73 }
74
75 /**//// <summary>
76 /// 获取数据库连接
77 /// </summary>
78 public IDbConnection Connection
79 {
80 get {
81 return _conn;
82 }
83 }
84 /**//// <summary>
85 /// 事务
86 /// </summary>
87 public IDbTransaction Transaction
88 {
89 get { return _transaction; }
90 }
91 #endregion
92
93 事务处理#region 事务处理
94 /**//// <summary>
95 /// 事务开始
96 /// </summary>
97 public void BeginTransaction()
98 {
99 if (_conn != null)
100 {
101 if (_conn.State == ConnectionState.Closed)
102 _conn.Open();
103 _transaction = _conn.BeginTransaction();
104 }
105 else
106 {
107 throw new Exception("未设置连接!");
108 }
109 }
110
111 /**//// <summary>
112 /// 事务提交
113 /// </summary>
114 public void CommitTransaction()
115 {
116 _transaction.Commit();
117 _trancomms.Clear();
118 }
119
120 /**//// <summary>
121 /// 事务回滚
122 /// </summary>
123 public void RollbackTransaction()
124 {
125 _transaction.Rollback();
126 _trancomms.Clear();
127 }
128 /**//// <summary>
129 /// IDbCommand 对象的克隆
130 /// </summary>
131 /// <param name="comm"></param>
132 /// <returns></returns>
133 private IDbCommand DeepClone(IDbCommand comm)
134 {
135 if (comm != null)
136 {
137 IDbCommand command = comm.Connection.CreateCommand();
138 command.CommandText = comm.CommandText;
139 command.CommandTimeout = comm.CommandTimeout;
140 command.CommandType = comm.CommandType;
141 foreach (IDataParameter p in comm.Parameters)
142 {
143 IDataParameter p2 = command.CreateParameter();
p2.ParameterName = p.ParameterName;
p2.DbType = p.DbType;
p2.Value = p.Value;
command.Parameters.Add(p2);
144 }
145 return command;
146 }
147 else
148 {
149 return null;
150 }
151 }
152 #endregion
153
154 创建查询#region 创建查询
155 /**//// <summary>
156 /// CreateQuery(string sql)
157 /// </summary>
158 /// <param name="sql">string sql</param>
159 /// <returns>DataHelper</returns>
160 public DataHelper CreateQuery(string sql)
161 {
162 if (_conn == null)
163 throw new Exception("请先设置数据库连接!");
164 _comm = _conn.CreateCommand();
165 _comm.CommandText = sql;
166 if(_conn.State == ConnectionState.Closed)
167 _conn.Open();
168 return this;
169 }
170
171 /**//// <summary>
172 /// CreateQuery(IDbConnection conn, string sql, CommandType commandType)
173 /// </summary>
174 /// <param name="conn">IDbConnection conn</param>
175 /// <param name="sql">string sql</param>
176 /// <param name="commandType">CommandType commandType</param>
177 /// <returns>DataHelper</returns>
178 public DataHelper CreateQuery(string sql, CommandType commandType)
179 {
180 _type = commandType;
181 return CreateQuery(sql);
182 }
183 #endregion
184
185 执行查询#region 执行查询
186 /**//// <summary>
187 /// 执行事务操作查询
188 /// </summary>
189 /// <returns>int val</returns>
190 public int ExecuteTransactionNoneQuery()
191 {
192 if (_transaction != null)
193 {
194 AddParameterToCommand();
195 IDbCommand comm = DeepClone(_comm);
196 _trancomms.Add(comm);
197 _comm.Dispose();
198 _comm = null;
199 comm.Transaction = _transaction;
200 return comm.ExecuteNonQuery();
201 }
202 else
203 {
204 throw new Exception("事务未打开!");
205 }
206 }
207
208 /**//// <summary>
209 /// ExecuteNonQuery()
210 /// Exceptions:
211 /// System.InvalidOperationException
212 /// </summary>
213 /// <returns>int</returns>
214 public int ExecuteNonQuery()
215 {
216 AddParameterToCommand();
217 try
218 {
219 int result = _comm.ExecuteNonQuery();
220 return result;
221 }
222 catch (InvalidOperationException ex)
223 {
224 if (_conn != null && _conn.State == ConnectionState.Open)
225 _conn.Close();
226 throw ex;
227 }
228 catch (Exception ex)
229 {
230 if (_conn != null && _conn.State == ConnectionState.Open)
231 _conn.Close();
232 throw ex;
233 }
234 }
235
236 /**//// <summary>
237 /// ExecuteScalar()
238 /// </summary>
239 /// <returns>object</returns>
240 public object ExecuteScalar()
241 {
242 AddParameterToCommand();
243 try
244 {
245 object obj = _comm.ExecuteScalar();
246 return obj;
247 }
248 catch (Exception ex)
249 {
250 if(_conn.State == ConnectionState.Open)
251 _conn.Close();
252 throw ex;
253 }
254 }
255
256 /**//// <summary>
257 /// ExecuteReader()
258 /// </summary>
259 /// <returns>IDataReader</returns>
260 public IDataReader ExecuteReader()
261 {
262 AddParameterToCommand();
263 try
264 {
265 return _comm.ExecuteReader();
266 }
267 catch (Exception ex)
268 {
269 if (_conn != null && _conn.State == ConnectionState.Open)
270 _conn.Close();
271 throw ex;
272 }
273 }
274
275 /**//// <summary>
276 /// ExecuteReader(CommandBehavior commandBehavior)
277 /// </summary>
278 /// <returns>IDataReader</returns>
279 public IDataReader ExecuteReader(CommandBehavior commandBehavior)
280 {
281 AddParameterToCommand();
282 try
283 {
284 return _comm.ExecuteReader(commandBehavior);
285 }
286 catch (Exception ex)
287 {
288 if (_conn != null && _conn.State == ConnectionState.Open)
289 _conn.Close();
290 throw ex;
291 }
292 }
293 /**//// <summary>
294 /// ExecuteDataSet(MyDbType myDbType)
295 /// </summary>
296 /// <returns>DataSet</returns>
297 public DataSet ExecuteDataSet()
298 {
299 AddParameterToCommand();
300 if (_conn != null && _conn.State == ConnectionState.Open)
301 _conn.Close();
302 DataSet ds = new DataSet();
303 switch (_myDbType)
304 {
305 case MyDbType.ORACLE:
306 using(OracleDataAdapter adapter = new OracleDataAdapter((OracleCommand)_comm))
307 {
308 adapter.Fill(ds);
309 }
310 break;
311 case MyDbType.MSSQL:
312 using(SqlDataAdapter adapter = new SqlDataAdapter((SqlCommand)_comm))
313 {
314 adapter.Fill(ds);
315 }
316 break;
317 }
318 return ds;
319 }
320
321 /**//// <summary>
322 /// ExecuteDataSet(int startRecord, int maxRecords, string srcTable)
323 /// </summary>
324 /// <param name="startRecord">int startRecord</param>
325 /// <param name="maxRecords">int maxRecords</param>
326 /// <param name="srcTable">string srcTable</param>
327 /// <returns>DataSet</returns>
328 public DataSet ExecuteDataSet(int startRecord, int maxRecords, string srcTable)
329 {
330 AddParameterToCommand();
331 if (_conn != null && _conn.State == ConnectionState.Open)
332 _conn.Close();
333 DataSet ds = new DataSet();
334 switch (_myDbType)
335 {
336 case MyDbType.ORACLE:
337 using (OracleDataAdapter adapter = new OracleDataAdapter((OracleCommand)_comm))
338 {
339 adapter.Fill(ds, startRecord, maxRecords, srcTable);
340 }
341 break;
342 case MyDbType.MSSQL:
343 using (SqlDataAdapter adapter = new SqlDataAdapter((SqlCommand)_comm))
344 {
345 adapter.Fill(ds, startRecord, maxRecords, srcTable);
346 }
347 break;
348 }
349 return ds;
350 }
351
352 #endregion
353
354 设置参数#region 设置参数
355 /**//// <summary>
356 /// 将设置好的参数加到Command中去
357 /// </summary>
358 private void AddParameterToCommand()
359 {
360 _comm.Parameters.Clear();
361 if(_type == CommandType.StoredProcedure)
362 {
363 foreach(IDataParameter p in _pars)
364 {
365 _comm.Parameters.Add(p);
366 }
367 }
368 else
369 {
370 string sql = _comm.CommandText;
371 string spliter = _myDbType == MyDbType.MSSQL ? "@" : ":";
372 string pattern = string.Format(@"{0}\w+", spliter);
373 System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(pattern, RegexOptions.IgnoreCase);
374 MatchCollection col = reg.Matches(sql);
375 foreach(Match item in col)
376 {
377 string val = item.Value.ToLower();
378 foreach(IDataParameter p in _pars)
379 {
380 if(p.ParameterName.ToLower() == val && !_comm.Parameters.Contains(val))
381 {
382 _comm.Parameters.Add(p);
383 break;
384 }
385 }
386 }
387 }
388 _pars.Clear();
389 }
390
391 /**//// <summary>
392 /// 设置参数
393 /// </summary>
394 /// <param name="name"></param>
395 /// <param name="dbType"></param>
396 /// <param name="value"></param>
397 /// <returns></returns>
398 private IDataParameter AddParameter(string name, DbType dbType, object value)
399 {
400 if (_comm != null)
401 {
402 IDataParameter p = _comm.CreateParameter();
403 p.ParameterName = _myDbType.Equals(MyDbType.MSSQL) ? string.Format("@{0}", name) : string.Format(":{0}", name);
404 p.DbType = dbType;
405 p.Value = value;
406 _pars.Add(p);
407 return p;
408 }
409 else
410 {
411 throw new Exception("IDbCommand对象为空!");
412 }
413 }
414
415 /**//// <summary>
416 /// 设置参数
417 /// </summary>
418 /// <param name="name"></param>
419 /// <param name="dbType"></param>
420 /// <param name="value"></param>
421 /// <returns></returns>
422 private IDataParameter AddParameter(string name, DbType dbType, object value, ParameterDirection parameterDirection)
423 {
424 IDataParameter p = AddParameter(name, dbType, value);
425 if(_type == CommandType.StoredProcedure)
426 {
427 p.Direction = parameterDirection;
428 }
429 return p;
430 }
431
432 /**//// <summary>
433 /// SetString(string name, string val)
434 /// </summary>
435 /// <param name="name">string name</param>
436 /// <param name="val">string val</param>
437 /// <returns>DataHelper</returns>
438 public DataHelper SetString(string name, string val)
439 {
440 AddParameter(name, DbType.String, (val == null ? string.Empty : val));
441 return this;
442 }
443
444 /**//// <summary>
445 /// SetString(string name, string val, ParameterDirection parameterDirection)
446 /// </summary>
447 /// <param name="name">string name</param>
448 /// <param name="val">string val</param>
449 /// <param name="parameterDirection">ParameterDirection parameterDirection</param>
450 /// <returns>DataHelper</returns>
451 public DataHelper SetString(string name, string val, ParameterDirection parameterDirection)
452 {
453 AddParameter(name, DbType.String, val, parameterDirection);
454 return this;
455 }
456
457 public DataHelper SetDouble(string name, double val)
458 {
459 AddParameter(name, DbType.Double, val);
460 return this;
461 }
462
463 public DataHelper SetDouble(string name, double val, ParameterDirection parameterDirection)
464 {
465 AddParameter(name, DbType.Double, parameterDirection);
466 return this;
467 }
468
469 public DataHelper SetDecimal(string name, decimal val)
470 {
471 AddParameter(name, DbType.Decimal, val);
472 return this;
473 }
474
475 public DataHelper SetDecimal(string name, decimal val, ParameterDirection parameterDirection)
476 {
477 AddParameter(name, DbType.Decimal, val, parameterDirection);
478 return this;
479 }
480
481 /**//// <summary>
482 /// SetInt32(string name, Int32 val)
483 /// </summary>
484 /// <param name="name">string name</param>
485 /// <param name="val">Int32 val</param>
486 /// <returns>DataHelper</returns>
487 public DataHelper SetInt32(string name, Int32 val)
488 {
489 AddParameter(name, DbType.Int32, val);
490 return this;
491 }
492
493 /**//// <summary>
494 /// SetInt32(string name, Int32 val, ParameterDirection parameterDirection)
495 /// </summary>
496 /// <param name="name">string name</param>
497 /// <param name="val">Int32 va</param>
498 /// <param name="parameterDirection">ParameterDirection parameterDirection</param>
499 /// <returns>DataHelper</returns>
500 public DataHelper SetInt32(string name, Int32 val, ParameterDirection parameterDirection)
501 {
502 AddParameter(name, DbType.Int32, val, parameterDirection);
503 return this;
504 }
505
506 /**//// <summary>
507 /// SetInt16(string name, Int16 val)
508 /// </summary>
509 /// <param name="name">string name</param>
510 /// <param name="val">Int16 val</param>
511 /// <returns>DataHelper</returns>
512 public DataHelper SetInt16(string name, Int16 val)
513 {
514 AddParameter(name, DbType.Int16, val);
515 return this;
516 }
517
518 /**//// <summary>
519 /// SetInt16(string name, Int16 val, ParameterDirection parameterDirection)
520 /// </summary>
521 /// <param name="name">string name</param>
522 /// <param name="val">Int16 val</param>
523 /// <param name="parameterDirection">ParameterDirection parameterDirection</param>
524 /// <returns>DataHelper</returns>
525 public DataHelper SetInt16(string name, Int16 val, ParameterDirection parameterDirection)
526 {
527 AddParameter(name, DbType.Int16, val, parameterDirection);
528 return this;
529 }
530
531 /**//// <summary>
532 /// SetDate(string name, DateTime val)
533 /// </summary>
534 /// <param name="name">string name</param>
535 /// <param name="val">DateTime val</param>
536 /// <returns>DataHelper</returns>
537 public DataHelper SetDate(string name, DateTime val)
538 {
539 AddParameter(name, DbType.Date, val);
540 return this;
541 }
542
543 /**//// <summary>
544 /// SetDate(string name, DateTime val, ParameterDirection parameterDirection)
545 /// </summary>
546 /// <param name="name">string name</param>
547 /// <param name="val">DateTime val</param>
548 /// <param name="parameterDirection">ParameterDirection parameterDirection</param>
549 /// <returns>DataHelper</returns>
550 public DataHelper SetDate(string name, DateTime val, ParameterDirection parameterDirection)
551 {
552 AddParameter(name, DbType.Date, val, parameterDirection);
553 return this;
554 }
555
556 /**//// <summary>
557 /// SetDateTime(string name, DateTime val)
558 /// </summary>
559 /// <param name="name">string name</param>
560 /// <param name="val">DateTime val</param>
561 /// <returns>DataHelper</returns>
562 public DataHelper SetDateTime(string name, DateTime val)
563 {
564 AddParameter(name, DbType.DateTime, val);
565 return this;
566 }
567
568 /**//// <summary>
569 /// SetDateTime(string name, DateTime val, ParameterDirection parameterDirection)
570 /// </summary>
571 /// <param name="name">string name</param>
572 /// <param name="val">DateTime val</param>
573 /// <param name="parameterDirection">ParameterDirection parameterDirection</param>
574 /// <returns>DataHelper</returns>
575 public DataHelper SetDateTime(string name, DateTime val, ParameterDirection parameterDirection)
576 {
577 AddParameter(name, DbType.DateTime, val, parameterDirection);
578 return this;
579 }
580
581 /**//// <summary>
582 /// SetEnum(string name, Enum val)
583 /// </summary>
584 /// <param name="name">string name</param>
585 /// <param name="val">Enum val</param>
586 /// <returns>DataHelper</returns>
587 public DataHelper SetEnum(string name, Enum val)
588 {
589 AddParameter(name, DbType.Int32, val.GetHashCode());
590 return this;
591 }
592
593 /**//// <summary>
594 /// SetEnum(string name, Enum val, ParameterDirection parameterDirection)
595 /// </summary>
596 /// <param name="name">string name</param>
597 /// <param name="val">Enum val</param>
598 /// <param name="parameterDirection">ParameterDirection parameterDirection</param>
599 /// <returns>DataHelper</returns>
600 public DataHelper SetEnum(string name, Enum val, ParameterDirection parameterDirection)
601 {
602 AddParameter(name, DbType.Int32, val.GetHashCode(), parameterDirection);
603 return this;
604 }
605
606 /**//// <summary>
607 /// SetEnum(string name, bool val)
608 /// </summary>
609 /// <param name="name">string name</param>
610 /// <param name="val">bool val</param>
611 /// <returns>DataHelper</returns>
612 public DataHelper SetBoolean(string name, bool val)
613 {
614 AddParameter(name, DbType.Byte, (val ? 1 : 0));
615 return this;
616 }
617
618 /**//// <summary>
619 /// SetEnum(string name, bool val, ParameterDirection parameterDirection)
620 /// </summary>
621 /// <param name="name">string name</param>
622 /// <param name="val">bool val</param>
623 /// <param name="parameterDirection">ParameterDirection parameterDirection</param>
624 /// <returns>DataHelper</returns>
625 public DataHelper SetBoolean(string name, bool val, ParameterDirection parameterDirection)
626 {
627 AddParameter(name, DbType.Byte, (val ? 1 : 0), parameterDirection);
628 return this;
629 }
630 #endregion
631
632 IDisposable 成员#region IDisposable 成员
633
634 public void Dispose()
635 {
636 if(_comm != null)
637 _comm.Dispose();
638 if (_trancomms != null)
639 {
640 for (int i = 0; i < _trancomms.Count; i++)
641 {
642 if((IDbCommand)_trancomms[i] != null)
643 ((IDbCommand)_trancomms[i]).Dispose();
644 }
645 }
646
647 if(_conn != null)
648 _conn.Dispose();
649 }
650
651 #endregion
652 }
653}
654
655