1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Data;
5using System.Data.SqlClient;
6using System.Data.OleDb;
7
8namespace IDataAccessLib
9{
10 类型选择#region 类型选择
11 public enum DBAccessType
12 {
13 SQL, OLEDB
14 }
15 #endregion
16
17 工厂类#region 工厂类
18 public class Factory
19 {
20 SQL工厂#region SQL工厂
21 public static IDataAccess GetSqlAccess(DBAccessType Type)
22 {
23 IDataAccess DBAccess = null;
24 switch (Type)
25 {
26 case DBAccessType.SQL:
27 {
28 DBAccess = new SqlAccess();
29 }
30 break;
31 }
32
33 return DBAccess;
34 }
35
36 public static IDataAccess GetSqlAccess(DBAccessType Type, string ConnectionString)
37 {
38 IDataAccess DBAccess = null;
39 switch (Type)
40 {
41 case DBAccessType.SQL:
42 {
43 DBAccess = new SqlAccess(ConnectionString);
44 }
45 break;
46 }
47
48 return DBAccess;
49 }
50 #endregion
51
52 OLEDB工厂#region OLEDB工厂
53 public static IDataAccess GetOledbAccess(DBAccessType Type)
54 {
55 IDataAccess DBAccess = null;
56 switch (Type)
57 {
58 case DBAccessType.OLEDB:
59 {
60 DBAccess = new OledbAccess();
61 }
62 break;
63 }
64 return DBAccess;
65 }
66
67 public static IDataAccess GetOledbAccess(DBAccessType Type, string ConnectionString)
68 {
69 IDataAccess DBAccess = null;
70 switch (Type)
71 {
72 case DBAccessType.OLEDB:
73 {
74 DBAccess = new OledbAccess(ConnectionString);
75 }
76 break;
77 }
78 return DBAccess;
79 }
80 #endregion
81 }
82 #endregion
83
84 接口#region 接口
85 public interface IDataAccess
86 {
87 string ConnectionString
88 {
89 get;
90 set;
91 }
92
93 void Open();
94 void Close();
95 void ExcuteCommand(string CommandString, IDataParameter[] Paras);
96 DataSet GetDataSetFromExcuteCommand(string CommandString, string TableName, IDataParameter[] Paras);
97 void ExecuteProc(string ProcName, IDataParameter[] Paras);
98 DataSet GetDataSetFromExcuteProc(string ProcName, string TableName, IDataParameter[] Paras);
99 object ExecuteCommandScalar(string CommandString, IDataParameter[] Paras);
100 object ExecuteProcScalar(string ProcName, IDataParameter[] Paras);
101 IDataReader GetDataReaderFromExcuteCommand(string CommandString, IDataParameter[] Paras);
102 IDataReader GetDataReaderFromExcuteProc(string ProcName, IDataParameter[] Paras);
103 }
104 #endregion
105
106 SQL类#region SQL类
107 public class SqlAccess : IDataAccess
108 {
109 IDataAccess 成员#region IDataAccess 成员
110
111 private string strConn;
112 private SqlConnection conn;
113
114 无参数构造#region 无参数构造
115 public SqlAccess()
116 {
117 strConn = "";
118 conn = new SqlConnection();
119 }
120 #endregion
121
122 有参数构造#region 有参数构造
123 public SqlAccess(string ConnectionString)
124 {
125 strConn = ConnectionString;
126 conn = new SqlConnection(strConn);
127 }
128 #endregion
129
130 返回第一行第一列方法#region 返回第一行第一列方法
131 public object GetScalar(SqlCommand cmd)
132 {
133 object obj = null;
134 try
135 {
136 Open();
137 obj = cmd.ExecuteScalar();
138 }
139 catch (Exception exp)
140 {
141 throw (new Exception("获取第一行第一列数据错误:" + exp.Message));
142 }
143 finally
144 {
145 Close();
146 }
147 return obj;
148 }
149 #endregion
150
151 数据库连接字符串属性器#region 数据库连接字符串属性器
152 public string ConnectionString
153 {
154 get
155 {
156 return strConn;
157 }
158 set
159 {
160 strConn = value;
161 conn.ConnectionString = strConn;
162 }
163 }
164 #endregion
165
166 打开数据库#region 打开数据库
167 public void Open()
168 {
169 try
170 {
171 if (conn.State != ConnectionState.Open) // 判断数据库连接状态是否打开
172 {
173 conn.Open(); // 打开数据库连接
174 }
175 }
176 catch (Exception exp)
177 {
178 throw (new Exception("打开数据库连接错误:" + exp.Message));
179 }
180 }
181 #endregion
182
183 关闭数据库#region 关闭数据库
184 public void Close()
185 {
186 try
187 {
188 if (conn.State != ConnectionState.Closed) // 判断数据库连接是否关闭
189 {
190 conn.Close(); // 关闭数据库连接
191 }
192 }
193 catch (Exception exp)
194 {
195 throw (new Exception("关闭数据库连接错误:" + exp.Message));
196 }
197 }
198 #endregion
199
200 执行命令#region 执行命令
201 public void ExcuteCommand(string CommandString, IDataParameter[] Paras)
202 {
203 try
204 {
205 SqlCommand cmd = new SqlCommand(CommandString, conn);
206 if (Paras != null)
207 {
208 foreach (IDataParameter Para in Paras)
209 {
210 cmd.Parameters.Add(Para);
211 }
212 }
213 Open();
214 cmd.ExecuteNonQuery();
215 }
216 catch (System.Exception exp)
217 {
218 throw (new System.Exception("执行命令错误:" + exp.Message));
219 }
220 finally
221 {
222 Close();
223 }
224 }
225 #endregion
226
227 执行命令返回DataSet#region 执行命令返回DataSet
228 public DataSet GetDataSetFromExcuteCommand(string CommandString, string TableName, IDataParameter[] Paras)
229 {
230 try
231 {
232 SqlCommand cmd = new SqlCommand(CommandString, conn);
233 if (Paras != null)
234 {
235 foreach (IDataParameter Para in Paras)
236 {
237 cmd.Parameters.Add(Para);
238 }
239 }
240
241 SqlDataAdapter da = new SqlDataAdapter(cmd);
242 DataSet ds = new DataSet();
243 da.Fill(ds, TableName);
244 return ds;
245 }
246 catch (System.Exception exp)
247 {
248 throw (new System.Exception("执行命令返回DataSet:" + exp.Message));
249 }
250 }
251 #endregion
252
253 执行存储过程#region 执行存储过程
254 public void ExecuteProc(string ProcName, IDataParameter[] Paras)
255 {
256 try
257 {
258 SqlCommand cmd = new SqlCommand();
259 cmd.Connection = conn;
260 cmd.CommandType = CommandType.StoredProcedure;
261 cmd.CommandText = ProcName;
262
263 if (Paras != null)
264 {
265 foreach (IDataParameter Para in Paras)
266 {
267 cmd.Parameters.Add(Para);
268 }
269 }
270
271 Open();
272 cmd.ExecuteNonQuery();
273 }
274 catch (System.Exception exp)
275 {
276 throw (new System.Exception("执行存储过程错误:" + exp.Message));
277 }
278 finally
279 {
280 Close();
281 }
282 }
283 #endregion
284
285 执行存储过程返回DataSet#region 执行存储过程返回DataSet
286 public DataSet GetDataSetFromExcuteProc(string ProcName, string TableName, IDataParameter[] Paras)
287 {
288 try
289 {
290 SqlCommand cmd = new SqlCommand();
291 cmd.Connection = conn;
292 cmd.CommandType = CommandType.StoredProcedure;
293 cmd.CommandText = ProcName;
294
295 if (Paras != null)
296 {
297 foreach (IDataParameter Para in Paras)
298 {
299 cmd.Parameters.Add(Para);
300 }
301 }
302
303 SqlDataAdapter da = new SqlDataAdapter(cmd);
304 DataSet ds = new DataSet();
305 da.Fill(ds, TableName);
306 return ds;
307 }
308 catch (System.Exception exp)
309 {
310 throw (new System.Exception("执行存储过程返回数据记录集错误:" + exp.Message));
311 }
312 }
313 #endregion
314
315 执行命令返回第一行第一列#region 执行命令返回第一行第一列
316 public object ExecuteCommandScalar(string CommandString, IDataParameter[] Paras)
317 {
318 SqlCommand cmd = new SqlCommand(CommandString, conn);
319 if (Paras != null)
320 {
321 foreach (IDataParameter Para in Paras)
322 {
323 cmd.Parameters.Add(Para);
324 }
325 }
326 object obj = null;
327 try
328 {
329 Open();
330 obj = cmd.ExecuteScalar();
331 return obj;
332 }
333 catch (System.Exception exp)
334 {
335 throw (new System.Exception("执行命令返回第一行第一列错误:" + exp.Message));
336 }
337 finally
338 {
339 Close();
340 }
341 }
342 #endregion
343
344 执行存储过程返回第一行第一列#region 执行存储过程返回第一行第一列
345 public object ExecuteProcScalar(string ProcName, IDataParameter[] Paras)
346 {
347 try
348 {
349 SqlCommand cmd = new SqlCommand();
350 cmd.Connection = conn;
351 cmd.CommandType = CommandType.StoredProcedure;
352 cmd.CommandText = ProcName;
353
354 if (Paras != null)
355 {
356 foreach (IDataParameter Para in Paras)
357 {
358 cmd.Parameters.Add(Para);
359 }
360 }
361
362 object obj = null;
363 obj = GetScalar(cmd);
364 return obj;
365 }
366 catch (System.Exception exp)
367 {
368 throw (new System.Exception("执行存储过程返回第一行第一列错误:" + exp.Message));
369 }
370 }
371 #endregion
372
373 执行命令返回IDataReader#region 执行命令返回IDataReader
374 public IDataReader GetDataReaderFromExcuteCommand(string CommandString, IDataParameter[] Paras)
375 {
376 try
377 {
378 SqlCommand cmd = new SqlCommand(CommandString, conn);
379 if (Paras != null)
380 {
381 foreach (IDataParameter Para in Paras)
382 {
383 cmd.Parameters.Add(Para);
384 }
385 }
386
387 SqlDataReader Reader = null;
388
389 Open();
390 Reader = cmd.ExecuteReader();
391 return Reader;
392 }
393 catch (System.Exception exp)
394 {
395 throw (new System.Exception("执行命令返回数据读取器错误:" + exp.Message));
396 }
397 finally
398 {
399 Close();
400 }
401 }
402 #endregion
403
404 执行存储过程返回IDataReader#region 执行存储过程返回IDataReader
405 public IDataReader GetDataReaderFromExcuteProc(string ProcName, IDataParameter[] Paras)
406 {
407 try
408 {
409 SqlCommand cmd = new SqlCommand();
410 cmd.Connection = conn;
411 cmd.CommandType = CommandType.StoredProcedure;
412 cmd.CommandText = ProcName;
413 if (Paras != null)
414 {
415 foreach (IDataParameter Para in Paras)
416 {
417 cmd.Parameters.Add(Para);
418 }
419 }
420
421 SqlDataReader Reader = null;
422
423 Open();
424 Reader = cmd.ExecuteReader();
425 return Reader;
426 }
427 catch (System.Exception exp)
428 {
429 throw (new System.Exception("执行存储过程返回数据读取器错误:" + exp.Message));
430 }
431 finally
432 {
433 Close();
434 }
435 }
436 #endregion
437
438 #endregion
439 }
440 #endregion
441
442 OLEDB类#region OLEDB类
443 public class OledbAccess : IDataAccess
444 {
445 IDataAccess 成员#region IDataAccess 成员
446
447 private string strConn;
448 private OleDbConnection conn;
449
450 无参数构造#region 无参数构造
451 public OledbAccess()
452 {
453 strConn = "";
454 conn = new OleDbConnection();
455 }
456 #endregion
457
458 有参数构造#region 有参数构造
459 public OledbAccess(string ConnectionString)
460 {
461 strConn = ConnectionString;
462 conn = new OleDbConnection(strConn);
463 }
464 #endregion
465
466 返回第一行第一列方法#region 返回第一行第一列方法
467 public object GetScalar(OleDbCommand cmd)
468 {
469 object obj = null;
470 try
471 {
472 Open();
473 obj = cmd.ExecuteScalar();
474 }
475 catch (Exception exp)
476 {
477 throw (new Exception("获取第一行第一列数据错误:" + exp.Message));
478 }
479 finally
480 {
481 Close();
482 }
483 return obj;
484 }
485 #endregion
486
487 数据库连接字符串属性器#region 数据库连接字符串属性器
488 public string ConnectionString
489 {
490 get
491 {
492 return strConn;
493 }
494 set
495 {
496 strConn = value;
497 conn.ConnectionString = strConn;
498 }
499 }
500 #endregion
501
502 打开数据库#region 打开数据库
503 public void Open()
504 {
505 try
506 {
507 if (conn.State != ConnectionState.Open) // 判断数据库连接状态是否打开
508 {
509 conn.Open(); // 打开数据库连接
510 }
511 }
512 catch (Exception exp)
513 {
514 throw (new Exception("打开数据库连接错误:" + exp.Message));
515 }
516 }
517 #endregion
518
519 关闭数据库#region 关闭数据库
520 public void Close()
521 {
522 try
523 {
524 if (conn.State != ConnectionState.Closed) // 判断数据库连接是否关闭
525 {
526 conn.Close(); // 关闭数据库连接
527 }
528 }
529 catch (Exception exp)
530 {
531 throw (new Exception("关闭数据库连接错误:" + exp.Message));
532 }
533 }
534 #endregion
535
536 执行命令#region 执行命令
537 public void ExcuteCommand(string CommandString, IDataParameter[] Paras)
538 {
539 try
540 {
541 OleDbCommand cmd = new OleDbCommand(CommandString, conn);
542 if (Paras != null)
543 {
544 foreach (IDataParameter Para in Paras)
545 {
546 cmd.Parameters.Add(Para);
547 }
548 }
549 Open();
550 cmd.ExecuteNonQuery();
551 }
552 catch (System.Exception exp)
553 {
554 throw (new System.Exception("执行命令错误:" + exp.Message));
555 }
556 finally
557 {
558 Close();
559 }
560 }
561 #endregion
562
563 执行命令返回DataSet#region 执行命令返回DataSet
564 public DataSet GetDataSetFromExcuteCommand(string CommandString, string TableName, IDataParameter[] Paras)
565 {
566 try
567 {
568 OleDbCommand cmd = new OleDbCommand(CommandString, conn);
569 if (Paras != null)
570 {
571 foreach (IDataParameter Para in Paras)
572 {
573 cmd.Parameters.Add(Para);
574 }
575 }
576
577 OleDbDataAdapter da = new OleDbDataAdapter(cmd);
578 DataSet ds = new DataSet();
579 da.Fill(ds, TableName);
580 return ds;
581 }
582 catch (System.Exception exp)
583 {
584 throw (new System.Exception("执行命令返回DataSet:" + exp.Message));
585 }
586 }
587 #endregion
588
589 执行存储过程#region 执行存储过程
590 public void ExecuteProc(string ProcName, IDataParameter[] Paras)
591 {
592 try
593 {
594 OleDbCommand cmd = new OleDbCommand();
595 cmd.Connection = conn;
596 cmd.CommandType = CommandType.StoredProcedure;
597 cmd.CommandText = ProcName;
598
599 if (Paras != null)
600 {
601 foreach (IDataParameter Para in Paras)
602 {
603 cmd.Parameters.Add(Para);
604 }
605 }
606
607 Open();
608 cmd.ExecuteNonQuery();
609 }
610 catch (System.Exception exp)
611 {
612 throw (new System.Exception("执行存储过程错误:" + exp.Message));
613 }
614 finally
615 {
616 Close();
617 }
618 }
619 #endregion
620
621 执行存储过程返回DataSet#region 执行存储过程返回DataSet
622 public DataSet GetDataSetFromExcuteProc(string ProcName, string TableName, IDataParameter[] Paras)
623 {
624 try
625 {
626 OleDbCommand cmd = new OleDbCommand();
627 cmd.Connection = conn;
628 cmd.CommandType = CommandType.StoredProcedure;
629 cmd.CommandText = ProcName;
630
631 if (Paras != null)
632 {
633 foreach (IDataParameter Para in Paras)
634 {
635 cmd.Parameters.Add(Para);
636 }
637 }
638
639 OleDbDataAdapter da = new OleDbDataAdapter(cmd);
640 DataSet ds = new DataSet();
641 da.Fill(ds, TableName);
642 return ds;
643 }
644 catch (System.Exception exp)
645 {
646 throw (new System.Exception("执行存储过程返回数据记录集错误:" + exp.Message));
647 }
648 }
649 #endregion
650
651 执行命令返回第一行第一列#region 执行命令返回第一行第一列
652 public object ExecuteCommandScalar(string CommandString, IDataParameter[] Paras)
653 {
654 OleDbCommand cmd = new OleDbCommand(CommandString, conn);
655 if (Paras != null)
656 {
657 foreach (IDataParameter Para in Paras)
658 {
659 cmd.Parameters.Add(Para);
660 }
661 }
662 object obj = null;
663 try
664 {
665 Open();
666 obj = cmd.ExecuteScalar();
667 return obj;
668 }
669 catch (System.Exception exp)
670 {
671 throw (new System.Exception("执行命令返回第一行第一列错误:" + exp.Message));
672 }
673 finally
674 {
675 Close();
676 }
677 }
678 #endregion
679
680 执行存储过程返回第一行第一列#region 执行存储过程返回第一行第一列
681 public object ExecuteProcScalar(string ProcName, IDataParameter[] Paras)
682 {
683 try
684 {
685 OleDbCommand cmd = new OleDbCommand();
686 cmd.Connection = conn;
687 cmd.CommandType = CommandType.StoredProcedure;
688 cmd.CommandText = ProcName;
689
690 if (Paras != null)
691 {
692 foreach (IDataParameter Para in Paras)
693 {
694 cmd.Parameters.Add(Para);
695 }
696 }
697
698 object obj = null;
699 obj = GetScalar(cmd);
700 return obj;
701 }
702 catch (System.Exception exp)
703 {
704 throw (new System.Exception("执行存储过程返回第一行第一列错误:" + exp.Message));
705 }
706 }
707 #endregion
708
709 执行命令返回IDataReader#region 执行命令返回IDataReader
710 public IDataReader GetDataReaderFromExcuteCommand(string CommandString, IDataParameter[] Paras)
711 {
712 try
713 {
714 OleDbCommand cmd = new OleDbCommand(CommandString, conn);
715 if (Paras != null)
716 {
717 foreach (IDataParameter Para in Paras)
718 {
719 cmd.Parameters.Add(Para);
720 }
721 }
722
723 OleDbDataReader Reader = null;
724
725 Open();
726 Reader = cmd.ExecuteReader();
727 return Reader;
728 }
729 catch (System.Exception exp)
730 {
731 throw (new System.Exception("执行命令返回数据读取器错误:" + exp.Message));
732 }
733 finally
734 {
735 Close();
736 }
737 }
738 #endregion
739
740 执行存储过程返回IDataReader#region 执行存储过程返回IDataReader
741 public IDataReader GetDataReaderFromExcuteProc(string ProcName, IDataParameter[] Paras)
742 {
743 try
744 {
745 OleDbCommand cmd = new OleDbCommand();
746 cmd.Connection = conn;
747 cmd.CommandType = CommandType.StoredProcedure;
748 cmd.CommandText = ProcName;
749 if (Paras != null)
750 {
751 foreach (IDataParameter Para in Paras)
752 {
753 cmd.Parameters.Add(Para);
754 }
755 }
756
757 OleDbDataReader Reader = null;
758
759 Open();
760 Reader = cmd.ExecuteReader();
761 return Reader;
762 }
763 catch (System.Exception exp)
764 {
765 throw (new System.Exception("执行存储过程返回数据读取器错误:" + exp.Message));
766 }
767 finally
768 {
769 Close();
770 }
771 }
772 #endregion
773
774 #endregion
775 }
776 #endregion
777}
2using System.Collections.Generic;
3using System.Text;
4using System.Data;
5using System.Data.SqlClient;
6using System.Data.OleDb;
7
8namespace IDataAccessLib
9{
10 类型选择#region 类型选择
11 public enum DBAccessType
12 {
13 SQL, OLEDB
14 }
15 #endregion
16
17 工厂类#region 工厂类
18 public class Factory
19 {
20 SQL工厂#region SQL工厂
21 public static IDataAccess GetSqlAccess(DBAccessType Type)
22 {
23 IDataAccess DBAccess = null;
24 switch (Type)
25 {
26 case DBAccessType.SQL:
27 {
28 DBAccess = new SqlAccess();
29 }
30 break;
31 }
32
33 return DBAccess;
34 }
35
36 public static IDataAccess GetSqlAccess(DBAccessType Type, string ConnectionString)
37 {
38 IDataAccess DBAccess = null;
39 switch (Type)
40 {
41 case DBAccessType.SQL:
42 {
43 DBAccess = new SqlAccess(ConnectionString);
44 }
45 break;
46 }
47
48 return DBAccess;
49 }
50 #endregion
51
52 OLEDB工厂#region OLEDB工厂
53 public static IDataAccess GetOledbAccess(DBAccessType Type)
54 {
55 IDataAccess DBAccess = null;
56 switch (Type)
57 {
58 case DBAccessType.OLEDB:
59 {
60 DBAccess = new OledbAccess();
61 }
62 break;
63 }
64 return DBAccess;
65 }
66
67 public static IDataAccess GetOledbAccess(DBAccessType Type, string ConnectionString)
68 {
69 IDataAccess DBAccess = null;
70 switch (Type)
71 {
72 case DBAccessType.OLEDB:
73 {
74 DBAccess = new OledbAccess(ConnectionString);
75 }
76 break;
77 }
78 return DBAccess;
79 }
80 #endregion
81 }
82 #endregion
83
84 接口#region 接口
85 public interface IDataAccess
86 {
87 string ConnectionString
88 {
89 get;
90 set;
91 }
92
93 void Open();
94 void Close();
95 void ExcuteCommand(string CommandString, IDataParameter[] Paras);
96 DataSet GetDataSetFromExcuteCommand(string CommandString, string TableName, IDataParameter[] Paras);
97 void ExecuteProc(string ProcName, IDataParameter[] Paras);
98 DataSet GetDataSetFromExcuteProc(string ProcName, string TableName, IDataParameter[] Paras);
99 object ExecuteCommandScalar(string CommandString, IDataParameter[] Paras);
100 object ExecuteProcScalar(string ProcName, IDataParameter[] Paras);
101 IDataReader GetDataReaderFromExcuteCommand(string CommandString, IDataParameter[] Paras);
102 IDataReader GetDataReaderFromExcuteProc(string ProcName, IDataParameter[] Paras);
103 }
104 #endregion
105
106 SQL类#region SQL类
107 public class SqlAccess : IDataAccess
108 {
109 IDataAccess 成员#region IDataAccess 成员
110
111 private string strConn;
112 private SqlConnection conn;
113
114 无参数构造#region 无参数构造
115 public SqlAccess()
116 {
117 strConn = "";
118 conn = new SqlConnection();
119 }
120 #endregion
121
122 有参数构造#region 有参数构造
123 public SqlAccess(string ConnectionString)
124 {
125 strConn = ConnectionString;
126 conn = new SqlConnection(strConn);
127 }
128 #endregion
129
130 返回第一行第一列方法#region 返回第一行第一列方法
131 public object GetScalar(SqlCommand cmd)
132 {
133 object obj = null;
134 try
135 {
136 Open();
137 obj = cmd.ExecuteScalar();
138 }
139 catch (Exception exp)
140 {
141 throw (new Exception("获取第一行第一列数据错误:" + exp.Message));
142 }
143 finally
144 {
145 Close();
146 }
147 return obj;
148 }
149 #endregion
150
151 数据库连接字符串属性器#region 数据库连接字符串属性器
152 public string ConnectionString
153 {
154 get
155 {
156 return strConn;
157 }
158 set
159 {
160 strConn = value;
161 conn.ConnectionString = strConn;
162 }
163 }
164 #endregion
165
166 打开数据库#region 打开数据库
167 public void Open()
168 {
169 try
170 {
171 if (conn.State != ConnectionState.Open) // 判断数据库连接状态是否打开
172 {
173 conn.Open(); // 打开数据库连接
174 }
175 }
176 catch (Exception exp)
177 {
178 throw (new Exception("打开数据库连接错误:" + exp.Message));
179 }
180 }
181 #endregion
182
183 关闭数据库#region 关闭数据库
184 public void Close()
185 {
186 try
187 {
188 if (conn.State != ConnectionState.Closed) // 判断数据库连接是否关闭
189 {
190 conn.Close(); // 关闭数据库连接
191 }
192 }
193 catch (Exception exp)
194 {
195 throw (new Exception("关闭数据库连接错误:" + exp.Message));
196 }
197 }
198 #endregion
199
200 执行命令#region 执行命令
201 public void ExcuteCommand(string CommandString, IDataParameter[] Paras)
202 {
203 try
204 {
205 SqlCommand cmd = new SqlCommand(CommandString, conn);
206 if (Paras != null)
207 {
208 foreach (IDataParameter Para in Paras)
209 {
210 cmd.Parameters.Add(Para);
211 }
212 }
213 Open();
214 cmd.ExecuteNonQuery();
215 }
216 catch (System.Exception exp)
217 {
218 throw (new System.Exception("执行命令错误:" + exp.Message));
219 }
220 finally
221 {
222 Close();
223 }
224 }
225 #endregion
226
227 执行命令返回DataSet#region 执行命令返回DataSet
228 public DataSet GetDataSetFromExcuteCommand(string CommandString, string TableName, IDataParameter[] Paras)
229 {
230 try
231 {
232 SqlCommand cmd = new SqlCommand(CommandString, conn);
233 if (Paras != null)
234 {
235 foreach (IDataParameter Para in Paras)
236 {
237 cmd.Parameters.Add(Para);
238 }
239 }
240
241 SqlDataAdapter da = new SqlDataAdapter(cmd);
242 DataSet ds = new DataSet();
243 da.Fill(ds, TableName);
244 return ds;
245 }
246 catch (System.Exception exp)
247 {
248 throw (new System.Exception("执行命令返回DataSet:" + exp.Message));
249 }
250 }
251 #endregion
252
253 执行存储过程#region 执行存储过程
254 public void ExecuteProc(string ProcName, IDataParameter[] Paras)
255 {
256 try
257 {
258 SqlCommand cmd = new SqlCommand();
259 cmd.Connection = conn;
260 cmd.CommandType = CommandType.StoredProcedure;
261 cmd.CommandText = ProcName;
262
263 if (Paras != null)
264 {
265 foreach (IDataParameter Para in Paras)
266 {
267 cmd.Parameters.Add(Para);
268 }
269 }
270
271 Open();
272 cmd.ExecuteNonQuery();
273 }
274 catch (System.Exception exp)
275 {
276 throw (new System.Exception("执行存储过程错误:" + exp.Message));
277 }
278 finally
279 {
280 Close();
281 }
282 }
283 #endregion
284
285 执行存储过程返回DataSet#region 执行存储过程返回DataSet
286 public DataSet GetDataSetFromExcuteProc(string ProcName, string TableName, IDataParameter[] Paras)
287 {
288 try
289 {
290 SqlCommand cmd = new SqlCommand();
291 cmd.Connection = conn;
292 cmd.CommandType = CommandType.StoredProcedure;
293 cmd.CommandText = ProcName;
294
295 if (Paras != null)
296 {
297 foreach (IDataParameter Para in Paras)
298 {
299 cmd.Parameters.Add(Para);
300 }
301 }
302
303 SqlDataAdapter da = new SqlDataAdapter(cmd);
304 DataSet ds = new DataSet();
305 da.Fill(ds, TableName);
306 return ds;
307 }
308 catch (System.Exception exp)
309 {
310 throw (new System.Exception("执行存储过程返回数据记录集错误:" + exp.Message));
311 }
312 }
313 #endregion
314
315 执行命令返回第一行第一列#region 执行命令返回第一行第一列
316 public object ExecuteCommandScalar(string CommandString, IDataParameter[] Paras)
317 {
318 SqlCommand cmd = new SqlCommand(CommandString, conn);
319 if (Paras != null)
320 {
321 foreach (IDataParameter Para in Paras)
322 {
323 cmd.Parameters.Add(Para);
324 }
325 }
326 object obj = null;
327 try
328 {
329 Open();
330 obj = cmd.ExecuteScalar();
331 return obj;
332 }
333 catch (System.Exception exp)
334 {
335 throw (new System.Exception("执行命令返回第一行第一列错误:" + exp.Message));
336 }
337 finally
338 {
339 Close();
340 }
341 }
342 #endregion
343
344 执行存储过程返回第一行第一列#region 执行存储过程返回第一行第一列
345 public object ExecuteProcScalar(string ProcName, IDataParameter[] Paras)
346 {
347 try
348 {
349 SqlCommand cmd = new SqlCommand();
350 cmd.Connection = conn;
351 cmd.CommandType = CommandType.StoredProcedure;
352 cmd.CommandText = ProcName;
353
354 if (Paras != null)
355 {
356 foreach (IDataParameter Para in Paras)
357 {
358 cmd.Parameters.Add(Para);
359 }
360 }
361
362 object obj = null;
363 obj = GetScalar(cmd);
364 return obj;
365 }
366 catch (System.Exception exp)
367 {
368 throw (new System.Exception("执行存储过程返回第一行第一列错误:" + exp.Message));
369 }
370 }
371 #endregion
372
373 执行命令返回IDataReader#region 执行命令返回IDataReader
374 public IDataReader GetDataReaderFromExcuteCommand(string CommandString, IDataParameter[] Paras)
375 {
376 try
377 {
378 SqlCommand cmd = new SqlCommand(CommandString, conn);
379 if (Paras != null)
380 {
381 foreach (IDataParameter Para in Paras)
382 {
383 cmd.Parameters.Add(Para);
384 }
385 }
386
387 SqlDataReader Reader = null;
388
389 Open();
390 Reader = cmd.ExecuteReader();
391 return Reader;
392 }
393 catch (System.Exception exp)
394 {
395 throw (new System.Exception("执行命令返回数据读取器错误:" + exp.Message));
396 }
397 finally
398 {
399 Close();
400 }
401 }
402 #endregion
403
404 执行存储过程返回IDataReader#region 执行存储过程返回IDataReader
405 public IDataReader GetDataReaderFromExcuteProc(string ProcName, IDataParameter[] Paras)
406 {
407 try
408 {
409 SqlCommand cmd = new SqlCommand();
410 cmd.Connection = conn;
411 cmd.CommandType = CommandType.StoredProcedure;
412 cmd.CommandText = ProcName;
413 if (Paras != null)
414 {
415 foreach (IDataParameter Para in Paras)
416 {
417 cmd.Parameters.Add(Para);
418 }
419 }
420
421 SqlDataReader Reader = null;
422
423 Open();
424 Reader = cmd.ExecuteReader();
425 return Reader;
426 }
427 catch (System.Exception exp)
428 {
429 throw (new System.Exception("执行存储过程返回数据读取器错误:" + exp.Message));
430 }
431 finally
432 {
433 Close();
434 }
435 }
436 #endregion
437
438 #endregion
439 }
440 #endregion
441
442 OLEDB类#region OLEDB类
443 public class OledbAccess : IDataAccess
444 {
445 IDataAccess 成员#region IDataAccess 成员
446
447 private string strConn;
448 private OleDbConnection conn;
449
450 无参数构造#region 无参数构造
451 public OledbAccess()
452 {
453 strConn = "";
454 conn = new OleDbConnection();
455 }
456 #endregion
457
458 有参数构造#region 有参数构造
459 public OledbAccess(string ConnectionString)
460 {
461 strConn = ConnectionString;
462 conn = new OleDbConnection(strConn);
463 }
464 #endregion
465
466 返回第一行第一列方法#region 返回第一行第一列方法
467 public object GetScalar(OleDbCommand cmd)
468 {
469 object obj = null;
470 try
471 {
472 Open();
473 obj = cmd.ExecuteScalar();
474 }
475 catch (Exception exp)
476 {
477 throw (new Exception("获取第一行第一列数据错误:" + exp.Message));
478 }
479 finally
480 {
481 Close();
482 }
483 return obj;
484 }
485 #endregion
486
487 数据库连接字符串属性器#region 数据库连接字符串属性器
488 public string ConnectionString
489 {
490 get
491 {
492 return strConn;
493 }
494 set
495 {
496 strConn = value;
497 conn.ConnectionString = strConn;
498 }
499 }
500 #endregion
501
502 打开数据库#region 打开数据库
503 public void Open()
504 {
505 try
506 {
507 if (conn.State != ConnectionState.Open) // 判断数据库连接状态是否打开
508 {
509 conn.Open(); // 打开数据库连接
510 }
511 }
512 catch (Exception exp)
513 {
514 throw (new Exception("打开数据库连接错误:" + exp.Message));
515 }
516 }
517 #endregion
518
519 关闭数据库#region 关闭数据库
520 public void Close()
521 {
522 try
523 {
524 if (conn.State != ConnectionState.Closed) // 判断数据库连接是否关闭
525 {
526 conn.Close(); // 关闭数据库连接
527 }
528 }
529 catch (Exception exp)
530 {
531 throw (new Exception("关闭数据库连接错误:" + exp.Message));
532 }
533 }
534 #endregion
535
536 执行命令#region 执行命令
537 public void ExcuteCommand(string CommandString, IDataParameter[] Paras)
538 {
539 try
540 {
541 OleDbCommand cmd = new OleDbCommand(CommandString, conn);
542 if (Paras != null)
543 {
544 foreach (IDataParameter Para in Paras)
545 {
546 cmd.Parameters.Add(Para);
547 }
548 }
549 Open();
550 cmd.ExecuteNonQuery();
551 }
552 catch (System.Exception exp)
553 {
554 throw (new System.Exception("执行命令错误:" + exp.Message));
555 }
556 finally
557 {
558 Close();
559 }
560 }
561 #endregion
562
563 执行命令返回DataSet#region 执行命令返回DataSet
564 public DataSet GetDataSetFromExcuteCommand(string CommandString, string TableName, IDataParameter[] Paras)
565 {
566 try
567 {
568 OleDbCommand cmd = new OleDbCommand(CommandString, conn);
569 if (Paras != null)
570 {
571 foreach (IDataParameter Para in Paras)
572 {
573 cmd.Parameters.Add(Para);
574 }
575 }
576
577 OleDbDataAdapter da = new OleDbDataAdapter(cmd);
578 DataSet ds = new DataSet();
579 da.Fill(ds, TableName);
580 return ds;
581 }
582 catch (System.Exception exp)
583 {
584 throw (new System.Exception("执行命令返回DataSet:" + exp.Message));
585 }
586 }
587 #endregion
588
589 执行存储过程#region 执行存储过程
590 public void ExecuteProc(string ProcName, IDataParameter[] Paras)
591 {
592 try
593 {
594 OleDbCommand cmd = new OleDbCommand();
595 cmd.Connection = conn;
596 cmd.CommandType = CommandType.StoredProcedure;
597 cmd.CommandText = ProcName;
598
599 if (Paras != null)
600 {
601 foreach (IDataParameter Para in Paras)
602 {
603 cmd.Parameters.Add(Para);
604 }
605 }
606
607 Open();
608 cmd.ExecuteNonQuery();
609 }
610 catch (System.Exception exp)
611 {
612 throw (new System.Exception("执行存储过程错误:" + exp.Message));
613 }
614 finally
615 {
616 Close();
617 }
618 }
619 #endregion
620
621 执行存储过程返回DataSet#region 执行存储过程返回DataSet
622 public DataSet GetDataSetFromExcuteProc(string ProcName, string TableName, IDataParameter[] Paras)
623 {
624 try
625 {
626 OleDbCommand cmd = new OleDbCommand();
627 cmd.Connection = conn;
628 cmd.CommandType = CommandType.StoredProcedure;
629 cmd.CommandText = ProcName;
630
631 if (Paras != null)
632 {
633 foreach (IDataParameter Para in Paras)
634 {
635 cmd.Parameters.Add(Para);
636 }
637 }
638
639 OleDbDataAdapter da = new OleDbDataAdapter(cmd);
640 DataSet ds = new DataSet();
641 da.Fill(ds, TableName);
642 return ds;
643 }
644 catch (System.Exception exp)
645 {
646 throw (new System.Exception("执行存储过程返回数据记录集错误:" + exp.Message));
647 }
648 }
649 #endregion
650
651 执行命令返回第一行第一列#region 执行命令返回第一行第一列
652 public object ExecuteCommandScalar(string CommandString, IDataParameter[] Paras)
653 {
654 OleDbCommand cmd = new OleDbCommand(CommandString, conn);
655 if (Paras != null)
656 {
657 foreach (IDataParameter Para in Paras)
658 {
659 cmd.Parameters.Add(Para);
660 }
661 }
662 object obj = null;
663 try
664 {
665 Open();
666 obj = cmd.ExecuteScalar();
667 return obj;
668 }
669 catch (System.Exception exp)
670 {
671 throw (new System.Exception("执行命令返回第一行第一列错误:" + exp.Message));
672 }
673 finally
674 {
675 Close();
676 }
677 }
678 #endregion
679
680 执行存储过程返回第一行第一列#region 执行存储过程返回第一行第一列
681 public object ExecuteProcScalar(string ProcName, IDataParameter[] Paras)
682 {
683 try
684 {
685 OleDbCommand cmd = new OleDbCommand();
686 cmd.Connection = conn;
687 cmd.CommandType = CommandType.StoredProcedure;
688 cmd.CommandText = ProcName;
689
690 if (Paras != null)
691 {
692 foreach (IDataParameter Para in Paras)
693 {
694 cmd.Parameters.Add(Para);
695 }
696 }
697
698 object obj = null;
699 obj = GetScalar(cmd);
700 return obj;
701 }
702 catch (System.Exception exp)
703 {
704 throw (new System.Exception("执行存储过程返回第一行第一列错误:" + exp.Message));
705 }
706 }
707 #endregion
708
709 执行命令返回IDataReader#region 执行命令返回IDataReader
710 public IDataReader GetDataReaderFromExcuteCommand(string CommandString, IDataParameter[] Paras)
711 {
712 try
713 {
714 OleDbCommand cmd = new OleDbCommand(CommandString, conn);
715 if (Paras != null)
716 {
717 foreach (IDataParameter Para in Paras)
718 {
719 cmd.Parameters.Add(Para);
720 }
721 }
722
723 OleDbDataReader Reader = null;
724
725 Open();
726 Reader = cmd.ExecuteReader();
727 return Reader;
728 }
729 catch (System.Exception exp)
730 {
731 throw (new System.Exception("执行命令返回数据读取器错误:" + exp.Message));
732 }
733 finally
734 {
735 Close();
736 }
737 }
738 #endregion
739
740 执行存储过程返回IDataReader#region 执行存储过程返回IDataReader
741 public IDataReader GetDataReaderFromExcuteProc(string ProcName, IDataParameter[] Paras)
742 {
743 try
744 {
745 OleDbCommand cmd = new OleDbCommand();
746 cmd.Connection = conn;
747 cmd.CommandType = CommandType.StoredProcedure;
748 cmd.CommandText = ProcName;
749 if (Paras != null)
750 {
751 foreach (IDataParameter Para in Paras)
752 {
753 cmd.Parameters.Add(Para);
754 }
755 }
756
757 OleDbDataReader Reader = null;
758
759 Open();
760 Reader = cmd.ExecuteReader();
761 return Reader;
762 }
763 catch (System.Exception exp)
764 {
765 throw (new System.Exception("执行存储过程返回数据读取器错误:" + exp.Message));
766 }
767 finally
768 {
769 Close();
770 }
771 }
772 #endregion
773
774 #endregion
775 }
776 #endregion
777}