![]()
Code
1
Code(下)
2![]()
1/**//**//**//// <summary>
3![]()
2 /**//// 数据库枚举类型
4![]()
3 /**//// </summary>
5
4 public enum DBProviderType
6![]()
5
{
7![]()
6 /**//**//**//// <summary>
8![]()
7 /**//// OleDb数据库
9![]()
8 /**//// </summary>
10
9 OleDb,
11![]()
10 /**//**//**//// <summary>
12![]()
11 /**//// ODBC连接
13![]()
12 /**//// </summary>
14
13 Odbc,
15![]()
14 /**//**//**//// <summary>
16![]()
15 /**//// SqlServer数据库
17![]()
16 /**//// </summary>
18
17 SqlServer,
19![]()
18 /**//**//**//// <summary>
20![]()
19 /**//// Oracle数据库
21![]()
20 /**//// </summary>
22
21 Oracle
23
22 }
24![]()
23 /**//**//**//// <summary>
25![]()
24 /**//// dbCreater数据库对象实例化基类
26![]()
25 /**//// </summary>
27
26 public abstract class dbCreater
28![]()
27
{
29
28 public static string GetProviderInvariantName(DBProviderType type)
30![]()
29
{
31
30 string providername = "";
32
31 switch (type)
33![]()
32
{
34
33 case DBProviderType.Odbc:
35
34 providername = "System.Data.Odbc";
36
35 break;
37
36 case DBProviderType.OleDb:
38
37 providername = "System.Data.OleDb";
39
38 break;
40
39 case DBProviderType.SqlServer:
41
40 providername = "System.Data.SqlClient";
42
41 break;
43
42 case DBProviderType.Oracle:
44
43 providername = "System.Data.OracleClient";
45
44 break;
46
45 default:
47
46 providername = "";
48
47 break;
49
48 }
50
49 return providername;
51
50 }
52
51 public static DBProviderType getDBProviderType(string providerInvariantName)
53![]()
52
{
54
53 DBProviderType dbpt = DBProviderType.SqlServer;
55
54 switch (providerInvariantName)
56![]()
55
{
57
56 case "System.Data.Odbc":
58
57 dbpt = DBProviderType.Odbc;
59
58 break;
60
59 case "System.Data.OleDb":
61
60 dbpt = DBProviderType.OleDb;
62
61 break;
63
62 case "System.Data.SqlClient":
64
63 dbpt = DBProviderType.SqlServer;
65
64 break;
66
65 case "System.Data.OracleClient":
67
66 dbpt = DBProviderType.Oracle;
68
67 break;
69
68 default:
70
69 dbpt=DBProviderType.SqlServer;
71
70 break;
72
71 }
73
72 return dbpt;
74
73 }
75
74 }
76![]()
75 /**//**//**//// <summary>
77![]()
76 /**//// 创建sqlbaseObject数据库操作对象的类继承自dbCreater
78![]()
77 /**//// </summary>
79
78 public class SqldbCreater:dbCreater
80![]()
79
{
81
80 private static baseObject _obj;
82
81 public static baseObject getbaseObject(string connectionString)
83![]()
82
{
84
83 _obj=new SqlbaseObject(DBProviderType.SqlServer,DbProviderFactories.GetFactory(GetProviderInvariantName(DBProviderType.SqlServer)), connectionString, true, false);
85
84 return _obj;
86
85 }
87
86 public static baseObject getbaseObject(string connectionString, bool AutoCloseCon, bool SingleCon)
88![]()
87
{
89
88 if (SingleCon)
90![]()
89
{
91
90 if (_obj == null)
92![]()
91
{
93
92 _obj=new SqlbaseObject(DBProviderType.SqlServer, DbProviderFactories.GetFactory(GetProviderInvariantName(DBProviderType.SqlServer)), connectionString, AutoCloseCon, SingleCon);
94
93 }
95
94 }
96
95 else
97![]()
96
{
98
97 _obj=new SqlbaseObject(DBProviderType.SqlServer, DbProviderFactories.GetFactory(GetProviderInvariantName(DBProviderType.SqlServer)), connectionString, AutoCloseCon, SingleCon);
99
98 }
100
99 return _obj;
101
100 }
102
101 public static baseObject getbaseObject(string providerInvariantName, string connectionString)
103![]()
102
{
104
103 _obj= new SqlbaseObject(getDBProviderType(providerInvariantName), DbProviderFactories.GetFactory(providerInvariantName), connectionString, true, false);
105
104 return _obj;
106
105 }
107
106 public static baseObject getbaseObject(string providerInvariantName, string connectionString, bool AutoCloseCon, bool SingleCon)
108![]()
107
{
109
108 if (SingleCon)
110![]()
109
{
111
110 if (_obj == null)
112![]()
111
{
113
112 _obj=new SqlbaseObject(getDBProviderType(providerInvariantName), DbProviderFactories.GetFactory(providerInvariantName), connectionString, AutoCloseCon, SingleCon);
114
113 }
115
114 }
116
115 else
117![]()
116
{
118
117 _obj=new SqlbaseObject(getDBProviderType(providerInvariantName), DbProviderFactories.GetFactory(providerInvariantName), connectionString, AutoCloseCon, SingleCon);
119
118 }
120
119 return _obj;
121
120 }
122
121 public static baseObject getbaseObject(DbProviderFactory dbFactory, string connectionString)
123![]()
122
{
124
123 _obj= new SqlbaseObject(DBProviderType.SqlServer, dbFactory, connectionString, true, false);
125
124 return _obj;
126
125 }
127
126 public static baseObject getbaseObject(DbProviderFactory dbFactory, string connectionString, bool AutoCloseCon, bool SingleCon)
128![]()
127
{
129
128 if (SingleCon)
130![]()
129
{
131
130 if (_obj == null)
132![]()
131
{
133
132 _obj=new SqlbaseObject(DBProviderType.SqlServer, dbFactory, connectionString, AutoCloseCon, SingleCon);
134
133 }
135
134 }
136
135 else
137![]()
136
{
138
137 _obj=new SqlbaseObject(DBProviderType.SqlServer, dbFactory, connectionString, AutoCloseCon, SingleCon);
139
138 }
140
139 return _obj;
141
140 }
142
141 }
143![]()
142 /**//**//**//// <summary>
144![]()
143 /**//// 创建accessbaseObject数据库操作对象的类继承自dbCreater
145![]()
144 /**//// </summary>
146
145 public class OledbCreater : dbCreater
147![]()
146
{
148
147 private static baseObject _obj;
149
148 public static baseObject getbaseObject(string connectionString)
150![]()
149
{
151
150 _obj= new AccessbaseObject(DBProviderType.OleDb, DbProviderFactories.GetFactory(GetProviderInvariantName(DBProviderType.OleDb)), connectionString, true, false);
152
151 return _obj;
153
152 }
154
153 public static baseObject getbaseObject(string connectionString, bool AutoCloseCon, bool SingleCon)
155![]()
154
{
156
155 if (SingleCon)
157![]()
156
{
158
157 if (_obj == null)
159![]()
158
{
160
159 _obj=new AccessbaseObject(DBProviderType.OleDb, DbProviderFactories.GetFactory(GetProviderInvariantName(DBProviderType.OleDb)), connectionString, AutoCloseCon, SingleCon);;
161
160 }
162
161 }
163
162 else
164![]()
163
{
165
164 _obj=new AccessbaseObject(DBProviderType.OleDb, DbProviderFactories.GetFactory(GetProviderInvariantName(DBProviderType.OleDb)), connectionString, AutoCloseCon, SingleCon);;
166
165 }
167
166 return _obj;
168
167 }
169
168 public static baseObject getbaseObject(string providerInvariantName, string connectionString)
170![]()
169
{
171
170 _obj= new AccessbaseObject(getDBProviderType(providerInvariantName), DbProviderFactories.GetFactory(providerInvariantName), connectionString, true, false);
172
171 return _obj;
173
172 }
174
173 public static baseObject getbaseObject(string providerInvariantName, string connectionString, bool AutoCloseCon, bool SingleCon)
175![]()
174
{
176
175 if (SingleCon)
177![]()
176
{
178
177 if (_obj == null)
179![]()
178
{
180
179 _obj=new AccessbaseObject(getDBProviderType(providerInvariantName), DbProviderFactories.GetFactory(providerInvariantName), connectionString, AutoCloseCon, SingleCon);
181
180 }
182
181 }
183
182 else
184![]()
183
{
185
184 _obj=new AccessbaseObject(getDBProviderType(providerInvariantName), DbProviderFactories.GetFactory(providerInvariantName), connectionString, AutoCloseCon, SingleCon);
186
185 }
187
186 return _obj;
188
187 }
189
188 public static baseObject getbaseObject(DbProviderFactory dbFactory, string connectionString)
190![]()
189
{
191
190 _obj= new AccessbaseObject(DBProviderType.OleDb, dbFactory, connectionString, true, false);
192
191 return _obj;
193
192 }
194
193 public static baseObject getbaseObject(DbProviderFactory dbFactory, string connectionString, bool AutoCloseCon, bool SingleCon)
195![]()
194
{
196
195 if (SingleCon)
197![]()
196
{
198
197 if (_obj == null)
199![]()
198
{
200
199 _obj=new AccessbaseObject(DBProviderType.OleDb, dbFactory, connectionString, AutoCloseCon, SingleCon);
201
200 }
202
201 }
203
202 else
204![]()
203
{
205
204 _obj=new AccessbaseObject(DBProviderType.OleDb, dbFactory, connectionString, AutoCloseCon, SingleCon);
206
205 }
207
206 return _obj;
208
207 }
209
208 }
210![]()
209 /**//**//**//// <summary>
211![]()
210 /**//// 创建oraclebaseObject数据库操作对象的类继承自baseCreater
212![]()
211 /**//// </summary>
213
212 public class OracleCreater : dbCreater
214![]()
213
{
215
214 public static baseObject _obj;
216
215 public static baseObject getbaseObject(string connectionString)
217![]()
216
{
218
217 _obj= new OraclebaseObject(DBProviderType.Oracle, DbProviderFactories.GetFactory(GetProviderInvariantName(DBProviderType.Oracle)), connectionString, true, false);
219
218 return _obj;
220
219 }
221
220 public static baseObject getbaseObject(string connectionString, bool AutoCloseCon, bool SingleCon)
222![]()
221
{
223
222 if (SingleCon)
224![]()
223
{
225
224 if (_obj == null)
226![]()
225
{
227
226 _obj= new OraclebaseObject(DBProviderType.Oracle, DbProviderFactories.GetFactory(GetProviderInvariantName(DBProviderType.Oracle)), connectionString, AutoCloseCon, SingleCon);
228
227 }
229
228 }
230
229 else
231![]()
230
{
232
231 _obj=new OraclebaseObject(DBProviderType.Oracle, DbProviderFactories.GetFactory(GetProviderInvariantName(DBProviderType.Oracle)), connectionString, AutoCloseCon, SingleCon);
233
232 }
234
233 return _obj;
235
234 }
236
235 public static baseObject getbaseObject(string providerInvariantName, string connectionString)
237![]()
236
{
238
237 _obj= new OraclebaseObject(getDBProviderType(providerInvariantName), DbProviderFactories.GetFactory(providerInvariantName), connectionString, true, false);
239
238 return _obj;
240
239 }
241
240 public static baseObject getbaseObject(string providerInvariantName, string connectionString, bool AutoCloseCon, bool SingleCon)
242![]()
241
{
243
242 if (SingleCon)
244![]()
243
{
245
244 if (_obj == null)
246![]()
245
{
247
246 _obj=new OraclebaseObject(getDBProviderType(providerInvariantName), DbProviderFactories.GetFactory(providerInvariantName), connectionString, AutoCloseCon, SingleCon);
248
247 }
249
248 }
250
249 else
251![]()
250
{
252
251 _obj=new OraclebaseObject(getDBProviderType(providerInvariantName), DbProviderFactories.GetFactory(providerInvariantName), connectionString, AutoCloseCon, SingleCon);
253
252 }
254
253 return _obj;
255
254 }
256
255 public static baseObject getbaseObject(DbProviderFactory dbFactory, string connectionString)
257![]()
256
{
258
257 _obj= new OraclebaseObject(DBProviderType.Oracle, dbFactory, connectionString, true, false);
259
258 return _obj;
260
259 }
261
260 public static baseObject getbaseObject(DbProviderFactory dbFactory, string connectionString, bool AutoCloseCon, bool SingleCon)
262![]()
261
{
263
262 if (SingleCon)
264![]()
263
{
265
264 if (_obj == null)
266![]()
265
{
267
266 _obj=new OraclebaseObject(DBProviderType.Oracle, dbFactory, connectionString, AutoCloseCon, SingleCon);
268
267 }
269
268 }
270
269 else
271![]()
270
{
272
271 _obj=new OraclebaseObject(DBProviderType.Oracle, dbFactory, connectionString, AutoCloseCon, SingleCon);
273
272 }
274
273 return _obj;
275
274 }
276
275 }
277
276
278
277工厂模式的调用方式如下:
279
278try
280![]()
279
{
281
280 baseObject objsql = SqldbCreater.getbaseObject(@"server=.;uid=sa;pwd=;database=pubs", true, true);
282
281 objsql.PlayTypeAndConnstring();
283
282 objsql.PlayTypeAndConnstring(objsql.getConnection().ConnectionString);
284
283 objsql.PlayTypeAndConnstring();
285
284 DataTable dt = objsql.ExecuteDataTable(@"select * from authors", "authors");
286
285 foreach (DataRow dr in dt.Rows)
287![]()
286
{
288
287 string colName = string.Empty;
289
288 for (int i = 0; i < dt.Columns.Count; i++)
290![]()
289
{
291
290 colName = dt.Columns[i].ColumnName;
292
291 Console.WriteLine(dr[colName].ToString());
293
292 }
294
293 }
295
294 objsql = SqldbCreater.getbaseObject(@"server=.;uid=sa;pwd=;database=northwind", true, false);
296
295 objsql.PlayTypeAndConnstring();
297
296 objsql.PlayTypeAndConnstring(objsql.getConnection().ConnectionString);
298
297 objsql.PlayTypeAndConnstring();
299
298 dt = objsql.ExecuteDataTable(@"select * from employees", "emp");
300
299 foreach (DataRow dr in dt.Rows)
301![]()
300
{
302
301 string colName = string.Empty;
303
302 for (int i = 0; i < dt.Columns.Count; i++)
304![]()
303
{
305
304 colName = dt.Columns[i].ColumnName;
306
305 Console.WriteLine(dr[colName].ToString());
307
306 }
308
307 }
309
308 baseObject objaccess = OledbCreater.getbaseObject(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\whgl.mdb;", true, false);
310
309 objaccess.PlayTypeAndConnstring();
311
310 objaccess.PlayTypeAndConnstring(objaccess.getConnection().ConnectionString);
312
311 objaccess.PlayTypeAndConnstring();
313
312 dt = objaccess.ExecuteDataTable(@"select * from gclb", "gclb");
314
313 foreach (DataRow dr in dt.Rows)
315![]()
314
{
316
315 string colName = string.Empty;
317
316 for (int i = 0; i < dt.Columns.Count; i++)
318![]()
317
{
319
318 colName = dt.Columns[i].ColumnName;
320
319 Console.WriteLine(dr[colName].ToString());
321
320 }
322
321 }
323
322
324
323 baseObject objoracle = OracleCreater.getbaseObject(@"user id=crm;data source=fonny;password=1", true, false);
325
324 objoracle.PlayTypeAndConnstring();
326
325 objoracle.PlayTypeAndConnstring(objoracle.getConnection().ConnectionString);
327
326 objoracle.PlayTypeAndConnstring();
328
327 dt = objoracle.ExecuteDataTable(@"select * from dm_dept", "dept");
329
328 foreach (DataRow dr in dt.Rows)
330![]()
329
{
331
330 string colName = string.Empty;
332
331 for (int i = 0; i < dt.Columns.Count; i++)
333![]()
332
{
334
333 colName = dt.Columns[i].ColumnName;
335
334 Console.WriteLine(dr[colName].ToString());
336
335 }
337
336 }
338
337 }
339
338 catch(Exception ex)
340![]()
339
{
341
340 Console.WriteLine(ex.Message);
342
341 }
343
344![]()
345
346
这里和大家分享和学习如何学IT!
posted @
2008-07-30 13:14
花香的蜂
阅读(
383)
评论()
收藏
举报