立志于开发稳定,高性能,容易维护的系统.
追求系统模块化设计,精简与强大的内核框架,可插拔的功能模块
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
2
/// 数据库类
3
/// </summary>
4
public class Db4SQL
5![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
6
private SqlConnection lConnection;
7
private bool lConnectionKeepOpenTf;
8
private string lConnectionStr;
9
private SqlConnection lConnectionWithTransaction;
10
private SqlTransaction lTransaction;
11
private int lTransactionCount;
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
14
///
15
/// </summary>
16
public Db4SQL()
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
18
this.lConnectionKeepOpenTf = false;
19
this.lTransactionCount = 0;
20
this.lConnectionStr = Global.Static.SQLDbConnectionStr;
21
}
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
24
/// 把两个SqlConnection连接关闭lConnection,lConnectionWithTransaction,同时重置连接字符串lConnectionStr=Global.Static.SQLDbConnectionStr
25
/// </summary>
26
public void ConnectionClear()
27![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
28
ConnectionStrInit();
29
if ((this.lConnection != null) && (this.lConnection.State == ConnectionState.Open))
30![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
31
this.lConnection.Close();
32
this.lConnection = null;
33
}
34
if ((this.lConnectionWithTransaction != null) && (this.lConnectionWithTransaction.State == ConnectionState.Open))
35![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
36
this.lConnectionWithTransaction.Close();
37
this.lConnectionWithTransaction = null;
38
}
39
}
40![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
42
/// 得到一个SqlConnection对象
43
/// </summary>
44
/// <param name="pInTransactionTf">是否开事务</param>
45
/// <returns></returns>
46
internal SqlConnection ConnectionGet(bool pInTransactionTf)
47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
48
if (pInTransactionTf)
49![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
50
if (this.lConnectionWithTransaction == null)
51![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
52
throw new Exception("TransactionBegin 未执行");
53
}
54
return this.lConnectionWithTransaction;
55
}
56
if (this.lConnectionKeepOpenTf)
57![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
58
if (this.lConnection == null)
59![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
60
this.lConnection = new SqlConnection(this.lConnectionStr);
61
}
62
if (this.lConnection.State != ConnectionState.Open)
63![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
64
this.lConnection.Open();
65
}
66
return this.lConnection;
67
}
68
SqlConnection connection = new SqlConnection(this.lConnectionStr);
69
connection.Open();
70
return connection;
71
}
72![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
74
/// 得到一个DataTable
75
/// </summary>
76
/// <param name="pSqlStr">要执行的字符串</param>
77
/// <returns></returns>
78
public System.Data.DataTable DataTableGet(string pSqlStr)
79![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
80
return this.DataTableGet(pSqlStr, false);
81
}
82![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
83![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
84
/// 得到一个DataTable
85
/// </summary>
86
/// <param name="pSqlStr">要执行的字符串</param>
87
/// <param name="pInTransactionTf">是否在事务中</param>
88
/// <returns></returns>
89
public System.Data.DataTable DataTableGet(string pSqlStr, bool pInTransactionTf)
90![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
91
SqlConnection connection = this.ConnectionGet(pInTransactionTf);
92
SqlCommand command = new SqlCommand(pSqlStr, connection);
93
if (pInTransactionTf)
94![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
95
command.Transaction = this.lTransaction;
96
}
97
command.CommandTimeout = 0x2710;//10000
98
command.CommandType = CommandType.Text;
99
SqlDataAdapter adapter = new SqlDataAdapter();
100
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
101
adapter.SelectCommand = command;
102
System.Data.DataTable dataTable = new System.Data.DataTable();
103
try
104![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
105
adapter.Fill(dataTable);
106
}
107
catch (System.Exception exception)
108![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
109
throw new Exception(exception.Message+" 执行(" + pSqlStr + ")时出错");
110
}
111
finally
112![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
113
if (!pInTransactionTf && !this.lConnectionKeepOpenTf)
114![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
115
connection.Close();
116
}
117
}
118
return dataTable;
119
}
120![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
121![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
122
/// 执行一条语句
123
/// </summary>
124
/// <param name="pSqlString"></param>
125
/// <returns>返回影响条数</returns>
126
public int Execute(string pSqlString)
127![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
128
return this.Execute(pSqlString, false);
129
}
130![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
131![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
132
/// 执行一条语句
133
/// </summary>
134
/// <param name="pSqlString"></param>
135
/// <param name="pInTransactionTf"></param>
136
/// <returns>返回影响条数</returns>
137
public int Execute(string pSqlString, bool pInTransactionTf)
138![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
139
SqlConnection connection = this.ConnectionGet(pInTransactionTf);
140
SqlCommand command = new SqlCommand(pSqlString, connection);
141
command.CommandTimeout = 0x2710;
142
if (pInTransactionTf)
143![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
144
command.Transaction = this.lTransaction;
145
}
146
try
147![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
148
return command.ExecuteNonQuery();
149
}
150
catch (System.Exception exception)
151![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
152
throw new Exception(exception.Message + "执行(" + pSqlString + ")时出错");
153
}
154
finally
155![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
156
if (!pInTransactionTf && !this.lConnectionKeepOpenTf)
157![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
158
connection.Close();
159
}
160
}
161
return 0;
162
}
163![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
164![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
165
/// 得到 0行0列 的值
166
/// </summary>
167
/// <param name="pSqlString"></param>
168
/// <returns></returns>
169
public object FindAField(string pSqlString)
170![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
171
return this.FindAField(pSqlString, false);
172
}
173![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
174![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
175
/// 得到 0行0列 的值
176
/// </summary>
177
/// <param name="pSqlString"></param>
178
/// <param name="pInTransactionTf"></param>
179
/// <returns></returns>
180
public object FindAField(string pSqlString, bool pInTransactionTf)
181![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
182
SqlCommand command = new SqlCommand(pSqlString, this.ConnectionGet(pInTransactionTf));
183
command.CommandTimeout = 0x2710;//10000
184
if (pInTransactionTf)
185![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
186
command.Transaction = this.lTransaction;
187
try
188![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
189
return command.ExecuteScalar();
190
}
191
catch (System.Exception exception)
192![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
193
throw new Exception(exception.Message + "执行(" + pSqlString + ")时出错");
194
}
195
}
196
else if (!this.lConnectionKeepOpenTf)
197![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
198
try
199![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
200
return command.ExecuteScalar();
201
}
202
catch (System.Exception exception2)
203![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
204
throw new Exception(exception2.Message+"执行(" + pSqlString + ")时出错");
205
}
206
finally
207![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
208
command.Connection.Close();
209
}
210
}
211
else
212![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
213
try
214![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
215
return command.ExecuteScalar();
216
}
217
catch (System.Exception exception3)
218![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
219
throw new Exception(exception3.Message+"执行(" + pSqlString + ")时出错");
220
}
221
}
222
return null;
223
}
224![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
225![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
226
/// 是否存在以pTableID为名称的表
227
/// </summary>
228
/// <param name="pTableID"></param>
229
/// <returns></returns>
230
public bool TableExistsTf(string pTableID)
231![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
232
string pSqlString = "select count(*) from sysobjects Where id = object_id(N'[" + pTableID + "]') And OBJECTPROPERTY(id, N'IsUserTable') = 1";
233
return (int.Parse(this.FindAField(pSqlString).ToString()) == 1);
234
}
235![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
236![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
237
/// 开始事务
238
/// </summary>
239
public void TransactionBegin()
240![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
241
if (this.lTransactionCount == 0)
242![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
243
this.lConnectionWithTransaction = new SqlConnection(this.ConnectionStr);
244
this.lConnectionWithTransaction.Open();
245
this.lTransaction = this.lConnectionWithTransaction.BeginTransaction();
246
}
247
this.lTransactionCount++;
248
}
249![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
250![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
251
/// 提交事务
252
/// </summary>
253
public void TransactionCommit()
254![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
255
if (this.lTransactionCount == 0)
256![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
257
throw new Exception("TransactionBegin未执行");
258
}
259
this.lTransactionCount--;
260
if (this.lTransactionCount == 0)
261![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
262
this.lTransaction.Commit();
263
this.lConnectionWithTransaction.Close();
264
this.lConnectionWithTransaction = null;
265
}
266
}
267![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
268![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
269
/// 回滚事务
270
/// </summary>
271
public void TransactionRollback()
272![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
273
if (this.lTransactionCount == 0)
274![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
275
throw new Exception("TransactionBegin未执行");
276
}
277
this.lTransactionCount--;
278
if (this.lTransactionCount == 0)
279![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
280
this.lTransaction.Rollback();
281
this.lConnectionWithTransaction.Close();
282
this.lConnectionWithTransaction = null;
283
}
284
}
285![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
286![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
287
/// 是否存在某视图
288
/// </summary>
289
/// <param name="pViewName"></param>
290
/// <returns></returns>
291
public bool ViewExistsTf(string pViewName)
292![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
293
string pSqlString = "select count(*) from sysobjects Where id = object_id(N'[" + pViewName + "]') And OBJECTPROPERTY(id, N'IsView') = 1";
294
return (int.Parse(this.FindAField(pSqlString).ToString()) == 1);
295
}
296![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
297![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
298![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
299
/// 是否把连接保持Open
300
/// </summary>
301
public bool ConnectionKeepOpenTf
302![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
303
get
304![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
305
return this.lConnectionKeepOpenTf;
306
}
307
set
308![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
309
this.lConnectionKeepOpenTf = value;
310
if ((!this.lConnectionKeepOpenTf && (this.lConnection != null)) && (this.lConnection.State == ConnectionState.Open))
311![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
312
this.lConnection.Close();
313
}
314
}
315
}
316![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
317![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
318
/// 连接字符串
319
/// </summary>
320
public string ConnectionStr
321![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
322
get
323![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
324
return this.lConnectionStr;
325
}
326
}
327![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
328![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
329
/// 初始化连接字符串lConnectionStr
330
/// </summary>
331
private void ConnectionStrInit()
332![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
333
this.lConnectionStr = Global.Static.SQLDbConnectionStr;
334
}
335![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
336![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
337
/// 事务
338
/// </summary>
339
internal SqlTransaction Transaction
340![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
341
get
342![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
343
return this.lTransaction;
344
}
345
}
346
}
347![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
348![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
349![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
350![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
351![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
352![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
353
/// 数据库类
354
/// </summary>
355
public class Db4ACCESS
356![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
357
private OleDbConnection lConnection;
358
private bool lConnectionKeepOpenTf;
359
private string lConnectionStr;
360
private OleDbConnection lConnectionWithTransaction;
361
private OleDbTransaction lTransaction;
362
private int lTransactionCount;
363![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
364![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
365
///
366
/// </summary>
367
public Db4ACCESS()
368![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
369
this.lConnectionKeepOpenTf = false;
370
this.lTransactionCount = 0;
371
this.lConnectionStr = Global.Static.ACCESSDbConnectionStr;
372
}
373![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
374![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
375
/// 把两个Connection连接关闭lConnection,lConnectionWithTransaction,同时重置连接字符串lConnectionStr=Global.Static.ACCESSDbConnectionStr
376
/// </summary>
377
public void ConnectionClear()
378![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
379
ConnectionStrInit();
380![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
381
if ((this.lConnection != null) && (this.lConnection.State == ConnectionState.Open))
382![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
383
this.lConnection.Close();
384
this.lConnection = null;
385
}
386
if ((this.lConnectionWithTransaction != null) && (this.lConnectionWithTransaction.State == ConnectionState.Open))
387![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
388
this.lConnectionWithTransaction.Close();
389
this.lConnectionWithTransaction = null;
390
}
391
}
392![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
393![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
394
/// 得到一个Connection对象
395
/// </summary>
396
/// <param name="pInTransactionTf">是否开事务</param>
397
/// <returns></returns>
398
internal OleDbConnection ConnectionGet(bool pInTransactionTf)
399![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
400
if (pInTransactionTf)
401![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
402
if (this.lConnectionWithTransaction == null)
403![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
404
throw new Exception("TransactionBegin 未执行");
405
}
406
return this.lConnectionWithTransaction;
407
}
408
if (this.lConnectionKeepOpenTf)
409![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
410
if (this.lConnection == null)
411![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
412
this.lConnection = new OleDbConnection(this.lConnectionStr);
413
}
414
if (this.lConnection.State != ConnectionState.Open)
415![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
416
this.lConnection.Open();
417
}
418
return this.lConnection;
419
}
420
OleDbConnection connection = new OleDbConnection(this.lConnectionStr);
421
connection.Open();
422
return connection;
423
}
424![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
425![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
426
/// 得到一个DataTable
427
/// </summary>
428
/// <param name="pSqlStr">要执行的字符串</param>
429
/// <returns></returns>
430
public System.Data.DataTable DataTableGet(string pSqlStr)
431![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
432
return this.DataTableGet(pSqlStr, false);
433
}
434![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
435![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
436
/// 得到一个DataTable
437
/// </summary>
438
/// <param name="pSqlStr">要执行的字符串</param>
439
/// <param name="pInTransactionTf">是否在事务中</param>
440
/// <returns></returns>
441
public System.Data.DataTable DataTableGet(string pSqlStr, bool pInTransactionTf)
442![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
443
OleDbConnection connection = this.ConnectionGet(pInTransactionTf);
444
OleDbCommand command = new OleDbCommand(pSqlStr, connection);
445
if (pInTransactionTf)
446![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
447
command.Transaction = this.lTransaction;
448
}
449
command.CommandTimeout = 0x2710;//10000
450
command.CommandType = CommandType.Text;
451
OleDbDataAdapter adapter = new OleDbDataAdapter();
452
OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);
453
adapter.SelectCommand = command;
454
System.Data.DataTable dataTable = new System.Data.DataTable();
455
try
456![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
457
adapter.Fill(dataTable);
458
}
459
catch (System.Exception exception)
460![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
461
throw new Exception(exception.Message+" 执行(" + pSqlStr + ")时出错");
462
}
463
finally
464![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
465
if (!pInTransactionTf && !this.lConnectionKeepOpenTf)
466![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
467
connection.Close();
468
}
469
}
470
return dataTable;
471
}
472![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
473![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
474
/// 执行一条语句
475
/// </summary>
476
/// <param name="pSqlString"></param>
477
/// <returns>返回影响条数</returns>
478
public int Execute(string pSqlString)
479![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
480
return this.Execute(pSqlString, false);
481
}
482![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
483![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
484
/// 执行一条语句
485
/// </summary>
486
/// <param name="pSqlString"></param>
487
/// <param name="pInTransactionTf"></param>
488
/// <returns>返回影响条数</returns>
489
public int Execute(string pSqlString, bool pInTransactionTf)
490![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
491
OleDbConnection connection = this.ConnectionGet(pInTransactionTf);
492
OleDbCommand command = new OleDbCommand(pSqlString, connection);
493
command.CommandTimeout = 0x2710;
494
if (pInTransactionTf)
495![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
496
command.Transaction = this.lTransaction;
497
}
498
try
499![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
500
return command.ExecuteNonQuery();
501
}
502
catch (System.Exception exception)
503![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
504
throw new Exception(exception.Message + "执行(" + pSqlString + ")时出错");
505
}
506
finally
507![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
508
if (!pInTransactionTf && !this.lConnectionKeepOpenTf)
509![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
510
connection.Close();
511
}
512
}
513
return 0;
514
}
515![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
516![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
517
/// 得到 0行0列 的值
518
/// </summary>
519
/// <param name="pSqlString"></param>
520
/// <returns></returns>
521
public object FindAField(string pSqlString)
522![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
523
return this.FindAField(pSqlString, false);
524
}
525![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
526![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
527
/// 得到 0行0列 的值
528
/// </summary>
529
/// <param name="pSqlString"></param>
530
/// <param name="pInTransactionTf"></param>
531
/// <returns></returns>
532
public object FindAField(string pSqlString, bool pInTransactionTf)
533![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
534
OleDbCommand command = new OleDbCommand(pSqlString, this.ConnectionGet(pInTransactionTf));
535
command.CommandTimeout = 0x2710;//10000
536
if (pInTransactionTf)
537![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
538
command.Transaction = this.lTransaction;
539
try
540![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
541
return command.ExecuteScalar();
542
}
543
catch (System.Exception exception)
544![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
545
throw new Exception(exception.Message + "执行(" + pSqlString + ")时出错");
546
}
547
}
548
else if (!this.lConnectionKeepOpenTf)
549![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
550
try
551![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
552
return command.ExecuteScalar();
553
}
554
catch (System.Exception exception2)
555![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
556
throw new Exception(exception2.Message+"执行(" + pSqlString + ")时出错");
557
}
558
finally
559![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
560
command.Connection.Close();
561
}
562
}
563
else
564![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
565
try
566![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
567
return command.ExecuteScalar();
568
}
569
catch (System.Exception exception3)
570![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
571
throw new Exception(exception3.Message+"执行(" + pSqlString + ")时出错");
572
}
573
}
574
return null;
575
}
576![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
577![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
578
/// 是否存在以pTableID为名称的表
579
/// </summary>
580
/// <param name="pTableID"></param>
581
/// <returns></returns>
582
public bool TableExistsTf(string pTableID)
583![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
584
string pSqlString = "select count(*) from "+pTableID;
585
try
586![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
587
this.Execute(pSqlString);
588
}
589
catch
590![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
591
return false;
592
}
593
return true;
594
}
595![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
596![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
597
/// 开始事务
598
/// </summary>
599
public void TransactionBegin()
600![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
601
if (this.lTransactionCount == 0)
602![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
603
this.lConnectionWithTransaction = new OleDbConnection(this.ConnectionStr);
604
this.lConnectionWithTransaction.Open();
605
this.lTransaction = this.lConnectionWithTransaction.BeginTransaction();
606
}
607
this.lTransactionCount++;
608
}
609![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
610![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
611
/// 提交事务
612
/// </summary>
613
public void TransactionCommit()
614![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
615
if (this.lTransactionCount == 0)
616![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
617
throw new Exception("TransactionBegin未执行");
618
}
619
this.lTransactionCount--;
620
if (this.lTransactionCount == 0)
621![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
622
this.lTransaction.Commit();
623
this.lConnectionWithTransaction.Close();
624
this.lConnectionWithTransaction = null;
625
}
626
}
627![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
628![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
629
/// 回滚事务
630
/// </summary>
631
public void TransactionRollback()
632![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
633
if (this.lTransactionCount == 0)
634![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
635
throw new Exception("TransactionBegin未执行");
636
}
637
this.lTransactionCount--;
638
if (this.lTransactionCount == 0)
639![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
640
this.lTransaction.Rollback();
641
this.lConnectionWithTransaction.Close();
642
this.lConnectionWithTransaction = null;
643
}
644
}
645![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
646![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
647![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
648
/// 是否把连接保持Open
649
/// </summary>
650
public bool ConnectionKeepOpenTf
651![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
652
get
653![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
654
return this.lConnectionKeepOpenTf;
655
}
656
set
657![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
658
this.lConnectionKeepOpenTf = value;
659
if ((!this.lConnectionKeepOpenTf && (this.lConnection != null)) && (this.lConnection.State == ConnectionState.Open))
660![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
661
this.lConnection.Close();
662
}
663
}
664
}
665![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
666![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
667
/// 连接字符串
668
/// </summary>
669
public string ConnectionStr
670![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
671
get
672![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
673
return this.lConnectionStr;
674
}
675
}
676![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
677![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
678
/// 初始化连接字符串lConnectionStr
679
/// </summary>
680
private void ConnectionStrInit()
681![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
682
this.lConnectionStr = Global.Static.ACCESSDbConnectionStr;
683
}
684![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
685![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
686
/// 事务
687
/// </summary>
688
internal OleDbTransaction Transaction
689![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
690
get
691![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
692
return this.lTransaction;
693
}
694
}
695![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
696
}
697![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
2
/// Global 的摘要说明。
3
/// </summary>
4
public class Static
5![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
6
private Static()
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
8
//
9
// TODO: 在此处添加构造函数逻辑
10
//
11
}
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
14
/// such as: server=127.0.0.1;database=tt;uid=sa;pwd=;
15
/// </summary>
16
public static string SQLDbConnectionStr="";
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
18
/// such as: PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=dataBasePath;Password=pwd;
19
/// </summary>
20
public static string ACCESSDbConnectionStr="";
21
}