1 using System;
2 using System.Configuration;
3 using System.Data;
4 using System.Data.SqlClient;
5
6 namespace SQLServerAccess
7 {
8 /// <summary>
9 /// 数据库连接管理类(SQLServer)
10 /// <!-- 注意:使用该类需要在配置文件中加入以下配置 -->
11 /// <!-- 数据库:连接字符串 -->
12 /// <add key="DbConnectionString" value="Data Source=地址;Initial Catalog=数据库名称;Persist Security Info=True;User ID=用户名;Password=密码;" />
13 /// <!-- 数据库:连接超时设置(秒) -->
14 /// <add key="DbConnectionTimeout" value="10" />
15 /// <!-- 数据库:SQL执行超时设置(秒) -->
16 /// <add key="DbCommandTimeout" value="30" />
17 /// </summary>
18 public class DbConnection : IDisposable
19 {
20 /// <summary>
21 /// 保持与数据库的连接
22 /// </summary>
23 private SqlConnection innerConnection = new SqlConnection();
24
25 /// <summary>
26 /// 事务对象
27 /// </summary>
28 private SqlTransaction innerTransaction;
29
30 /// <summary>
31 /// SQL命令对象
32 /// </summary>
33 private SqlCommand innerCommand;
34
35 /// <summary>
36 /// 获得连接
37 /// </summary>
38 public SqlConnection Connection
39 {
40 get { return this.innerConnection; }
41 }
42
43 /// <summary>
44 /// 返回一个命令
45 /// </summary>
46 public SqlCommand Command
47 {
48 get { return this.innerCommand; }
49 }
50
51 /// <summary>
52 /// 检索数据库的事务是否正在启动
53 /// </summary>
54 public bool IsTransaction
55 {
56 get
57 {
58 bool ret = false;
59 if (this.innerTransaction != null)
60 {
61 ret = (this.innerTransaction.Connection != null);
62 }
63 return ret;
64 }
65 }
66
67 /// <summary>
68 /// 创建数据库的连接对象并打开连接
69 /// </summary>
70 public void Open(string DBConnectionString = null)
71 {
72 if (string.IsNullOrEmpty(DBConnectionString))
73 {
74 // 默认的数据库
75 DBConnectionString = ConfigurationManager.AppSettings["DBConnectionString"];
76 }
77 // 数据库连接超时时间
78 int DbConnectionTimeout = 10;
79 if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["DbConnectionTimeout"]))
80 {
81 DbConnectionTimeout = int.Parse(ConfigurationManager.AppSettings["DbConnectionTimeout"]);
82 }
83 // SQL执行超时时间
84 int DbCommandTimeout = 30;
85 if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["DbCommandTimeout"]))
86 {
87 DbCommandTimeout = int.Parse(ConfigurationManager.AppSettings["DbCommandTimeout"]);
88 }
89
90 // 如果连接已打开,则表示错误
91 if (this.innerConnection.State != ConnectionState.Broken &&
92 this.innerConnection.State != ConnectionState.Closed)
93 {
94 //错误处理
95 throw new Exception("DBOpen Error.");
96 }
97
98 try
99 {
100 // 连接字符串设置
101 innerConnection.ConnectionString = DBConnectionString + ";Connection Timeout=" + DbConnectionTimeout;
102 // 打开连接
103 innerConnection.Open();
104 // 命令
105 innerCommand = innerConnection.CreateCommand();
106 innerCommand.CommandTimeout = DbCommandTimeout;
107 }
108 catch (Exception ex)
109 {
110 if (innerTransaction != null) innerTransaction.Dispose();
111 if (innerCommand != null) innerCommand.Dispose();
112 if (innerConnection != null) innerConnection.Dispose();
113 throw ex;
114 }
115 }
116
117 /// <summary>
118 /// 关闭与用户数据库的连接
119 /// </summary>
120 public void Close()
121 {
122 if (this.innerConnection != null &&
123 this.innerTransaction != null &&
124 this.innerTransaction.Connection != null)
125 {
126 try
127 {
128 this.innerTransaction.Rollback();
129 }
130 catch (Exception)
131 {
132 }
133 finally
134 {
135 this.innerTransaction = null;
136 }
137 }
138
139 try
140 {
141 this.innerConnection.Close();
142 this.innerConnection.Dispose();
143 this.innerTransaction = null;
144 }
145 catch (Exception)
146 {
147 }
148 finally
149 {
150 this.innerConnection = new SqlConnection();
151 }
152 }
153
154 /// <summary>
155 /// 事务开始
156 /// </summary>
157 public void BeginTran()
158 {
159 if (this.innerTransaction != null)
160 {
161 // 该事务已经开始
162 return;
163 }
164
165 try
166 {
167 // 开始事务
168 innerTransaction = innerConnection.BeginTransaction();
169 innerCommand.Transaction = innerTransaction;
170 }
171 catch (Exception ex)
172 {
173 throw ex;
174
175 }
176 }
177
178 /// <summary>
179 /// 事务提交
180 /// </summary>
181 public void Commit()
182 {
183 if (this.innerTransaction == null)
184 {
185 // 事务未开始
186 return;
187 }
188
189 try
190 {
191 this.innerTransaction.Commit();
192 this.innerTransaction = null;
193 }
194 catch (SqlException sqlex)
195 {
196 throw sqlex;
197 }
198 finally
199 {
200 if (this.innerTransaction != null &&
201 this.innerTransaction.Connection != null)
202 {
203 try
204 {
205 this.innerTransaction.Rollback();
206 this.innerTransaction = null;
207 }
208 catch (Exception) { }
209 }
210 }
211 }
212
213 /// <summary>
214 /// 事务回滚
215 /// </summary>
216 public void Rollback()
217 {
218 if (this.innerTransaction == null)
219 {
220 // 事务未开始
221 return;
222 }
223
224 try
225 {
226 this.innerTransaction.Rollback();
227 this.innerTransaction = null;
228 }
229 catch (SqlException sqlex)
230 {
231 throw sqlex;
232 }
233 }
234
235 /// <summary>
236 /// 构造函数
237 /// </summary>
238 public DbConnection(string DBConnectionString = null, bool openFlag = true)
239 {
240 if (openFlag)
241 {
242 Open(DBConnectionString);
243 }
244 }
245
246 /// <summary>
247 /// 释放资源
248 /// </summary>
249 public void Dispose()
250 {
251 Close();
252 }
253 }
254 }