偶得一个绝佳C#数据库封装类
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
本文由jsj05527贡献
偶得一个绝佳C#数据库封装类,与大家分享一下! - 楚广明的博客 - 博客园 楚广明的博客
教育不是灌溉,是点燃火焰
首页 联系 管理
偶得一个绝佳C#数据库封装类,与大家分享一下!
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Data;
5using System.Configuration;
6using System.Data.Common;
7using System.Data.SqlClient;
8using System.Data.OleDb;
9using System.Data.Odbc;
10using System.Data.OracleClient;
11using System.IO;
12
13namespace BinaryIntellect.DataAccess
14{
15 public class DatabaseHelper:IDisposable
16 {
17 private string strConnectionString;
18 private DbConnection objConnection;
19 private DbCommand objCommand;
20 private DbProviderFactory objFactory = null;
21 private bool boolHandleErrors;
22 private string strLastError;
23 private bool boolLogError;
24 private string strLogFile;
25
26 public DatabaseHelper(string connectionstring,Providers provider) 27 {
28 strConnectionString = connectionstring;
29 switch (provider)
30 {
31 case Providers.SqlServer:
32 objFactory = SqlClientFactory.Instance;
33 break;
34 case Providers.OleDb:
35 objFactory = OleDbFactory.Instance;
36 break;
37 case Providers.Oracle:
38 objFactory = OracleClientFactory.Instance;
39 break;
40 case Providers.ODBC:
41 objFactory = OdbcFactory.Instance;
42 break;
43 case Providers.ConfigDefined:
44 string providername=ConfigurationManager.ConnectionStrings["connec
45 switch (providername)
46 {
47 case "System.Data.SqlClient":
48 objFactory = SqlClientFactory.Instance;
49 break;
50 case "System.Data.OleDb":
51 objFactory = OleDbFactory.Instance;
52 break;
53 case "System.Data.OracleClient":
54 objFactory = OracleClientFactory.Instance;
55 break;
56 case "System.Data.Odbc":
57 objFactory = OdbcFactory.Instance;
58 break;
59 }
60 break;
61
62 }
63 objConnection = objFactory.CreateConnection();
64 objCommand = objFactory.CreateCommand();
65
66 objConnection.ConnectionString = strConnectionString;
67 objCommand.Connection = objConnection;
68 }
69
70 public DatabaseHelper(Providers provider):this(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString,provider)
71 {
72 }
73
74 public DatabaseHelper(string connectionstring): this(connectionstring, Providers.SqlServer)
75 {
76 }
77 public DatabaseHelper():this(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString,Providers.ConfigDefined)
79 {
80 }
81
82 public bool HandleErrors
83 {
84 get
85 {
86 return boolHandleErrors;
87 }
88 set
89 {
90 boolHandleErrors = value;
91 }
92 }
93