![](/Images/OutliningIndicators/ContractedBlock.gif)
Code
1![](/Images/OutliningIndicators/None.gif)
2
想了很久终于写了一个(ado)工厂模式上
3![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
4
/// baseObject基对象
5
/// </summary>
6
public abstract class baseObject
7![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
8
9
public DbProviderFactory dbFactory;//数据工厂以产生不同数据源操作对象
10
public string connectionString;//连接字
11
public DbConnection conn;//连接对象
12
public bool AutoCloseCon;//自动关闭标记
13
public bool SingleCon;//单一连接标记
14
public DBProviderType dbtype;
15
public baseObject(DBProviderType dbtype, DbProviderFactory dbFactory, string connectionString, bool AutoCloseCon, bool SingleCon)
16![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
17
this.dbtype = dbtype;
18
this.dbFactory = dbFactory;//实例化了数据源操作类(这个对象包含了DBProviderType)
19
this.connectionString = connectionString;//得到连接字会串
20
this.AutoCloseCon = AutoCloseCon;//得到自动关闭连接标记
21
this.SingleCon = SingleCon;//得到单一连接标记
22
}
23![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
24
/// 获得连接
25
/// </summary>
26
public DbConnection getConnection()
27![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
28
if (!SingleCon || conn == null)
29![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
30
conn = dbFactory.CreateConnection();
31
conn.ConnectionString = this.connectionString;
32
}
33
return conn;
34
}
35![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
36
/// 关闭连接
37
/// </summary>
38
public void closeConnection()
39![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
40
if (this.getConnection().State != ConnectionState.Closed)
41![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
42
this.getConnection().Close();
43
//this.getConnection().Dispose();
44
}
45
}
46![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
47
/// 根据DbCommand产生DataAdapter
48
/// </summary>
49
/// <param name="cmd">Command对像</param>
50
/// <returns>产生的DataAdapter对象</returns>
51
/// <permission cref="System.Security.PermissionSet">public</permission>
52
public DbDataAdapter GetDataAdapter(DbCommand cmd)
53![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
54
DbDataAdapter dbDataAdapter = this.dbFactory.CreateDataAdapter();
55
dbDataAdapter.SelectCommand = cmd;
56
return dbDataAdapter;
57
}
58![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
59
/// 取得DbCommand对象
60
/// </summary>
61
/// <returns>取得新的Command对象</returns>
62
/// <permission cref="System.Security.PermissionSet">public</permission>
63
public DbCommand GetCommand()
64![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
65
return this.dbFactory.CreateCommand();
66
}
67![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
68
/// 获取DbCommand对象
69
/// </summary>
70
/// <param name="cmdtext">cmd语句</param>
71
/// <returns>DbCommand</returns>
72
public DbCommand GetCommand(string cmdtext)
73![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
74
DbCommand cmd = GetCommand();
75
cmd.CommandText = cmdtext;
76
return cmd;
77
}
78![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
79
/// 获取和设置参数
80
/// </summary>
81
/// <param name="name">参数名</param>
82
/// <param name="dbtype">参数型</param>
83
/// <returns>参数</returns>
84
public DbParameter GetParameter(string name, DbType dbtype)
85![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
86
DbParameter dbp = this.dbFactory.CreateParameter();
87
dbp.ParameterName = name;
88
dbp.DbType = dbtype;
89
return dbp;
90
}
91![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
92
/// 获取和设置参数
93
/// </summary>
94
/// <param name="name">参数名</param>
95
/// <param name="dbtype">参数类型</param>
96
/// <param name="dbSize">参数大小</param>
97
/// <returns>参数</returns>
98
public DbParameter GetParameter(string name, DbType dbtype, int dbSize)
99![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
100
DbParameter dbp = GetParameter(name, dbtype);
101
dbp.Size = dbSize;
102
return dbp;
103
}
104![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
105
/// 根据DataAdapter生成DbCommandBuilder
106
/// </summary>
107
/// <param name="dataAdapter">DbDataAdapter对象</param>
108
/// <returns>取得新的Command对象</returns>
109
/// <permission cref="System.Security.PermissionSet">private</permission>
110
public DbCommandBuilder CommandBuilder(DbDataAdapter dataAdapter)
111![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
112
DbCommandBuilder cbd = this.dbFactory.CreateCommandBuilder();
113
cbd.DataAdapter = dataAdapter;
114
return cbd;
115
}
116![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
117
/// 不需要返回的数据库执行操作
118
/// </summary>
119
/// <param name="cmdText">执行语句</param>
120
/// <returns>cmd.ExecuteNonQuery(); 返回值</returns>
121
/// <permission cref="System.Security.PermissionSet">public</permission>
122
public int ExecuteNonQuery(string cmdText)
123![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
124
return ExecuteNonQuery(cmdText, null);
125
}
126![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
127
/// 不需要返回的数据库执行操作
128
/// </summary>
129
/// <param name="cmdText">执行语句</param>
130
/// <param name="cmdParms">存储过程参数</param>
131
/// <returns>cmd.ExecuteNonQuery(); 返回值</returns>
132
/// <permission cref="System.Security.PermissionSet">public</permission>
133
public int ExecuteNonQuery(string cmdText, DbParameter[] cmdParms)
134![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
135
DbConnection dbconn = getConnection();
136
DbCommand cmd = GetCommand();
137
using (cmd)
138![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
139
PrepareCommand(cmd, dbconn, cmdText, cmdParms);
140
int val = cmd.ExecuteNonQuery();
141
cmd.Parameters.Clear();
142
if (AutoCloseCon)
143![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
144
dbconn.Close();
145
}
146
return val;
147
}
148
}
149![](/Images/OutliningIndicators/InBlock.gif)
150![](/Images/OutliningIndicators/InBlock.gif)
151![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
152
/// 使用默认连接的ExecuteScalar
153
/// </summary>
154
/// <param name="cmdType">类型</param>
155
/// <param name="cmdText">执行语句</param>
156
/// <param name="cmdParms">存储过程参数</param>
157
/// <returns>object 返回值</returns>
158
/// <permission cref="System.Security.PermissionSet">public</permission>
159
public object ExecuteScalar(CommandType cmdType, string cmdText, params DbParameter[] cmdParms)
160![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
161
DbConnection dbconn = getConnection();
162
using (DbCommand cmd = GetCommand())
163![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
164
PrepareCommand(cmd, dbconn, null, cmdType, cmdText, cmdParms);
165
object val = cmd.ExecuteScalar();
166
cmd.Parameters.Clear();
167
if (AutoCloseCon)
168![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
169
dbconn.Close();
170
}
171
return val;
172
}
173
}
174![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
175
/// 使用默认连接的ExecuteScalar
176
/// </summary>
177
/// <param name="cmdText">执行语句</param>
178
/// <returns>object 返回值</returns>
179
/// <permission cref="System.Security.PermissionSet">public</permission>
180
public object ExecuteScalar(string cmdText)
181![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
182
return ExecuteScalar(CommandType.Text, cmdText, null);
183
}
184![](/Images/OutliningIndicators/InBlock.gif)
185![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
186
/// 初始化存储过程
187
/// </summary>
188
/// <param name="cmd">Command</param>
189
/// <param name="conn">连接</param>
190
/// <param name="trans">事务</param>
191
/// <param name="cmdType">类型</param>
192
/// <param name="cmdText">语句</param>
193
/// <param name="cmdParms">存储过程参数</param>
194
/// <permission cref="System.Security.PermissionSet">private</permission>
195
private void PrepareCommand(DbCommand cmd, DbConnection conn, DbTransaction trans, CommandType cmdType, string cmdText, DbParameter[] cmdParms)
196![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
197
if (conn.State != ConnectionState.Open)
198
conn.Open();
199
cmd.Connection = conn;
200
cmd.CommandText = cmdText;
201
202
if (trans != null)
203
cmd.Transaction = trans;
204
cmd.CommandType = cmdType;
205
if (cmdParms != null)
206![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
207
foreach (DbParameter parm in cmdParms)
208
cmd.Parameters.Add(parm);
209
}
210
}
211
private void PrepareCommand(DbCommand cmd, DbConnection conn, string cmdText, DbParameter[] cmdParms)
212![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
213
PrepareCommand(cmd, conn, null, CommandType.Text, cmdText, cmdParms);
214
}
215
private void PrepareCommand(DbCommand cmd, DbConnection conn, string cmdText)
216![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
217
PrepareCommand(cmd, conn, null, CommandType.Text, cmdText, null);
218
}
219![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
220
/// 取得DataReader
221
/// </summary>
222
/// <param name="cmdText">语句</param>
223
/// <param name="cmdParms">存储过程参数</param>
224
/// <returns>DataReader</returns>
225
/// <permission cref="System.Security.PermissionSet">public</permission>
226
public DbDataReader ExecuteReader(string cmdText, DbParameter[] cmdParms)
227![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
228
try
229![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
230
DbCommand cmd=GetCommand();
231
PrepareCommand(cmd, getConnection(), cmdText, cmdParms);
232
DbDataReader rdr;
233
if (AutoCloseCon)
234![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
235
rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
236
}
237
else
238![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
239
rdr = cmd.ExecuteReader(CommandBehavior.Default);
240
}
241
cmd.Parameters.Clear();
242
return rdr;
243
}
244
catch
245![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
246
throw;
247
}
248
}
249![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
250
/// 取得DataReader
251
/// </summary>
252
/// <param name="cmdText">语句</param>
253
/// <returns>DataReader</returns>
254
/// <permission cref="System.Security.PermissionSet">public</permission>
255
public DbDataReader ExecuteReader(string cmdText)
256![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
257
return ExecuteReader(cmdText, null);
258
}
259![](/Images/OutliningIndicators/InBlock.gif)
260![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
261
/// 获取DataSet
262
/// </summary>
263
/// <param name="cmdText">语句</param>
264
/// <param name="sTablename">DataTable名称</param>
265
/// <returns>DataSet</returns>
266
/// <permission cref="System.Security.PermissionSet">public</permission>
267
public DataSet ExecuteDataSet(string cmdText, string sTablename)
268![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
269
DataSet dst = new DataSet();
270
using (DbCommand cmd = GetCommand())
271![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
272
DbConnection dbconn = getConnection();
273
PrepareCommand(cmd, dbconn, cmdText, null);
274
using (DbDataAdapter adpt = GetDataAdapter(cmd))
275![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
276
adpt.Fill(dst, sTablename);
277
}
278
if (AutoCloseCon)
279![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
280
dbconn.Close();
281
}
282
}
283
return dst;
284
}
285![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
286
/// 获取DataTable
287
/// </summary>
288
/// <param name="cmdText">语句</param>
289
/// <param name="sTablename">DataTable名称</param>
290
/// <returns>DataTable</returns>
291
/// <permission cref="System.Security.PermissionSet">public</permission>
292
public DataTable ExecuteDataTable(string cmdText, string sTablename)
293![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
294
return ExecuteDataSet(cmdText, sTablename).Tables[sTablename];
295
}
296![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
297
/// 根据cmdText填充表
298
/// </summary>
299
/// <param name="dtb">要填充的表</param>
300
/// <param name="cmdText">查询语句</param>
301
/// <returns></returns>
302
public bool FillDataTable(DataTable dtb, string cmdText)
303![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
304
using (DbCommand cmd = GetCommand())
305![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
306
DbConnection dbconn = getConnection();
307
PrepareCommand(cmd, dbconn, cmdText, null);
308
using (DbDataAdapter adpt = GetDataAdapter(cmd))
309![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
310
adpt.Fill(dtb);
311
}
312![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (AutoCloseCon)
{ dbconn.Close(); }
313
}
314
return true;
315
}
316![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
317
/// 用DataTable更新数据库
318
/// </summary>
319
/// <param name="dtb">DataTable</param>
320
/// <param name="cmdText">selectcommandtext</param>
321
/// <returns>更新的行数</returns>
322
/// <permission cref="System.Security.PermissionSet">public</permission>
323
public int UpdateDataTable(DataTable dtb, string cmdText)
324![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
325
try
326![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
327
using (DbCommand com = GetCommand())
328![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
329
using (DbDataAdapter adapter = GetDataAdapter(com))
330![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
331
DbConnection dbconn = getConnection();
332
PrepareCommand(com, dbconn, cmdText);
333
adapter.SelectCommand = com;
334
DbCommandBuilder cbd = CommandBuilder(adapter);
335
adapter.InsertCommand = cbd.GetInsertCommand();
336
adapter.UpdateCommand = cbd.GetUpdateCommand();
337
int i = adapter.Update(dtb);
338
if (AutoCloseCon)
339![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
340
dbconn.Close();
341
}
342
return i;
343
}
344
}
345
}
346
catch
347![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
348
throw;
349
}
350
}
351![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
352
/// 用默认连接DataSet更新数据库
353
/// </summary>
354
/// <param name="dst">DataSet</param>
355
/// <param name="cmdText">selectcommandtext</param>
356
/// <param name="sTableName">表名</param>
357
/// <returns>更新的行数</returns>
358
/// <permission cref="System.Security.PermissionSet">public</permission>
359
public int UpdateDataSet(DataSet dst, string cmdText, string sTableName)
360![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
361
return UpdateDataTable(dst.Tables[sTableName], cmdText);
362
}
363![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
364
/// 用默认连接DataSet更新数据库
365
/// </summary>
366
/// <param name="dst">DataSet</param>
367
/// <param name="sTableName">表名</param>
368
/// <returns>更新的行数</returns>
369
/// <permission cref="System.Security.PermissionSet">public</permission>
370
public int UpdateDataSet(DataSet dst, string sTableName)
371![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
372
return UpdateDataTable(dst.Tables[sTableName], "select * from " + sTableName);
373
}
374![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
375
/// 获取表中下一ID
376
/// </summary>
377
/// <param name="sTable"></param>
378
/// <param name="sID"></param>
379
/// <returns></returns>
380
public string GetNextID(string sTable, string sID)
381![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
382
string sGID;
383
try
384![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
385
string sCt;
386
sCt = "select max(" + sID + ") as S_ID from " + sTable;
387
object o = ExecuteScalar(sCt);
388
if (o == null || o == DBNull.Value)
389![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
390
sGID = "1";
391
}
392
else
393![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
394
sGID = Convert.ToString(Convert.ToUInt32(o) + 1);
395
}
396
397
}
398
catch
399![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
400
sGID = "1";
401
}
402
return sGID;
403
}
404
405
public abstract string PlayTypeAndConnstring();
406
public abstract string PlayTypeAndConnstring(string s);
407
}
408![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
409
/// SQL数据操作对象继承自baseObject基对象
410
/// </summary>
411
public class SqlbaseObject : baseObject
412![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
413
public SqlbaseObject(DBProviderType dbtype,DbProviderFactory dbFactory, string connectionString, bool AutoCloseCon, bool SingleCon)
414
: base(dbtype,dbFactory, connectionString, AutoCloseCon, SingleCon)
415![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
416![](/Images/OutliningIndicators/InBlock.gif)
417
}
418
public override string PlayTypeAndConnstring()
419![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
420
Console.WriteLine(string.Format("{1}连接字符串是:{0}", this.connectionString,this.dbtype));
421
return connectionString;
422
}
423
public override string PlayTypeAndConnstring(string s)
424![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
425
Console.WriteLine(string.Format("{1}连接字符串是:{0}", s, this.dbtype));
426
return s;
427
}
428
}
429![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
430
/// Oracle数据库操作对象继承自baseObject基对象
431
/// </summary>
432
public class OraclebaseObject : baseObject
433![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
434
public OraclebaseObject(DBProviderType dbtype, DbProviderFactory dbFactory, string connectionString, bool AutoCloseCon, bool SingleCon)
435
: base(dbtype, dbFactory, connectionString, AutoCloseCon, SingleCon)
436![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
437![](/Images/OutliningIndicators/InBlock.gif)
438
}
439
public override string PlayTypeAndConnstring()
440![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
441
Console.WriteLine(string.Format("{1}连接字符串是:{0}", this.connectionString, this.dbtype));
442
return connectionString;
443
}
444
public override string PlayTypeAndConnstring(string s)
445![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
446
Console.WriteLine(string.Format("{1}连接字符串是:{0}", s, this.dbtype));
447
return s;
448
}
449
}
450![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
451
/// oledb数据库操作对象继承自baseObject基对象
452
/// </summary>
453
public class AccessbaseObject : baseObject
454![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
455
public AccessbaseObject(DBProviderType dbtype, DbProviderFactory dbFactory, string connectionString, bool AutoCloseCon, bool SingleCon)
456
: base(dbtype, dbFactory, connectionString, AutoCloseCon, SingleCon)
457![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
458![](/Images/OutliningIndicators/InBlock.gif)
459
}
460
public override string PlayTypeAndConnstring()
461![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
462
Console.WriteLine(string.Format("{1}连接字符串是:{0}", this.connectionString, this.dbtype));
463
return connectionString;
464
}
465
public override string PlayTypeAndConnstring(string s)
466![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
467
Console.WriteLine(string.Format("{1}连接字符串是:{0}", s, this.dbtype));
468
return s;
469
}
470
471
}
这里和大家分享和学习如何学IT!
posted @
2008-07-30 13:09
花香的蜂
阅读(
445)
评论()
编辑
收藏
举报