1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Data;
5
using System.Data.SqlClient;
6
using System.Data.OleDb;
7
8
namespace IDataAccessLib
9

{
10
类型选择#region 类型选择
11
public enum DBAccessType
12
{
13
SQL, OLEDB
14
}
15
#endregion
16
17
工厂类#region 工厂类
18
public class Factory
19
{
20
SQL工厂#region SQL工厂
21
public static IDataAccess GetSqlAccess(DBAccessType Type)
22
{
23
IDataAccess DBAccess = null;
24
switch (Type)
25
{
26
case DBAccessType.SQL:
27
{
28
DBAccess = new SqlAccess();
29
}
30
break;
31
}
32
33
return DBAccess;
34
}
35
36
public static IDataAccess GetSqlAccess(DBAccessType Type, string ConnectionString)
37
{
38
IDataAccess DBAccess = null;
39
switch (Type)
40
{
41
case DBAccessType.SQL:
42
{
43
DBAccess = new SqlAccess(ConnectionString);
44
}
45
break;
46
}
47
48
return DBAccess;
49
}
50
#endregion
51
52
OLEDB工厂#region OLEDB工厂
53
public static IDataAccess GetOledbAccess(DBAccessType Type)
54
{
55
IDataAccess DBAccess = null;
56
switch (Type)
57
{
58
case DBAccessType.OLEDB:
59
{
60
DBAccess = new OledbAccess();
61
}
62
break;
63
}
64
return DBAccess;
65
}
66
67
public static IDataAccess GetOledbAccess(DBAccessType Type, string ConnectionString)
68
{
69
IDataAccess DBAccess = null;
70
switch (Type)
71
{
72
case DBAccessType.OLEDB:
73
{
74
DBAccess = new OledbAccess(ConnectionString);
75
}
76
break;
77
}
78
return DBAccess;
79
}
80
#endregion
81
}
82
#endregion
83
84
接口#region 接口
85
public interface IDataAccess
86
{
87
string ConnectionString
88
{
89
get;
90
set;
91
}
92
93
void Open();
94
void Close();
95
void ExcuteCommand(string CommandString, IDataParameter[] Paras);
96
DataSet GetDataSetFromExcuteCommand(string CommandString, string TableName, IDataParameter[] Paras);
97
void ExecuteProc(string ProcName, IDataParameter[] Paras);
98
DataSet GetDataSetFromExcuteProc(string ProcName, string TableName, IDataParameter[] Paras);
99
object ExecuteCommandScalar(string CommandString, IDataParameter[] Paras);
100
object ExecuteProcScalar(string ProcName, IDataParameter[] Paras);
101
IDataReader GetDataReaderFromExcuteCommand(string CommandString, IDataParameter[] Paras);
102
IDataReader GetDataReaderFromExcuteProc(string ProcName, IDataParameter[] Paras);
103
}
104
#endregion
105
106
SQL类#region SQL类
107
public class SqlAccess : IDataAccess
108
{
109
IDataAccess 成员#region IDataAccess 成员
110
111
private string strConn;
112
private SqlConnection conn;
113
114
无参数构造#region 无参数构造
115
public SqlAccess()
116
{
117
strConn = "";
118
conn = new SqlConnection();
119
}
120
#endregion
121
122
有参数构造#region 有参数构造
123
public SqlAccess(string ConnectionString)
124
{
125
strConn = ConnectionString;
126
conn = new SqlConnection(strConn);
127
}
128
#endregion
129
130
返回第一行第一列方法#region 返回第一行第一列方法
131
public object GetScalar(SqlCommand cmd)
132
{
133
object obj = null;
134
try
135
{
136
Open();
137
obj = cmd.ExecuteScalar();
138
}
139
catch (Exception exp)
140
{
141
throw (new Exception("获取第一行第一列数据错误:" + exp.Message));
142
}
143
finally
144
{
145
Close();
146
}
147
return obj;
148
}
149
#endregion
150
151
数据库连接字符串属性器#region 数据库连接字符串属性器
152
public string ConnectionString
153
{
154
get
155
{
156
return strConn;
157
}
158
set
159
{
160
strConn = value;
161
conn.ConnectionString = strConn;
162
}
163
}
164
#endregion
165
166
打开数据库#region 打开数据库
167
public void Open()
168
{
169
try
170
{
171
if (conn.State != ConnectionState.Open) // 判断数据库连接状态是否打开
172
{
173
conn.Open(); // 打开数据库连接
174
}
175
}
176
catch (Exception exp)
177
{
178
throw (new Exception("打开数据库连接错误:" + exp.Message));
179
}
180
}
181
#endregion
182
183
关闭数据库#region 关闭数据库
184
public void Close()
185
{
186
try
187
{
188
if (conn.State != ConnectionState.Closed) // 判断数据库连接是否关闭
189
{
190
conn.Close(); // 关闭数据库连接
191
}
192
}
193
catch (Exception exp)
194
{
195
throw (new Exception("关闭数据库连接错误:" + exp.Message));
196
}
197
}
198
#endregion
199
200
执行命令#region 执行命令
201
public void ExcuteCommand(string CommandString, IDataParameter[] Paras)
202
{
203
try
204
{
205
SqlCommand cmd = new SqlCommand(CommandString, conn);
206
if (Paras != null)
207
{
208
foreach (IDataParameter Para in Paras)
209
{
210
cmd.Parameters.Add(Para);
211
}
212
}
213
Open();
214
cmd.ExecuteNonQuery();
215
}
216
catch (System.Exception exp)
217
{
218
throw (new System.Exception("执行命令错误:" + exp.Message));
219
}
220
finally
221
{
222
Close();
223
}
224
}
225
#endregion
226
227
执行命令返回DataSet#region 执行命令返回DataSet
228
public DataSet GetDataSetFromExcuteCommand(string CommandString, string TableName, IDataParameter[] Paras)
229
{
230
try
231
{
232
SqlCommand cmd = new SqlCommand(CommandString, conn);
233
if (Paras != null)
234
{
235
foreach (IDataParameter Para in Paras)
236
{
237
cmd.Parameters.Add(Para);
238
}
239
}
240
241
SqlDataAdapter da = new SqlDataAdapter(cmd);
242
DataSet ds = new DataSet();
243
da.Fill(ds, TableName);
244
return ds;
245
}
246
catch (System.Exception exp)
247
{
248
throw (new System.Exception("执行命令返回DataSet:" + exp.Message));
249
}
250
}
251
#endregion
252
253
执行存储过程#region 执行存储过程
254
public void ExecuteProc(string ProcName, IDataParameter[] Paras)
255
{
256
try
257
{
258
SqlCommand cmd = new SqlCommand();
259
cmd.Connection = conn;
260
cmd.CommandType = CommandType.StoredProcedure;
261
cmd.CommandText = ProcName;
262
263
if (Paras != null)
264
{
265
foreach (IDataParameter Para in Paras)
266
{
267
cmd.Parameters.Add(Para);
268
}
269
}
270
271
Open();
272
cmd.ExecuteNonQuery();
273
}
274
catch (System.Exception exp)
275
{
276
throw (new System.Exception("执行存储过程错误:" + exp.Message));
277
}
278
finally
279
{
280
Close();
281
}
282
}
283
#endregion
284
285
执行存储过程返回DataSet#region 执行存储过程返回DataSet
286
public DataSet GetDataSetFromExcuteProc(string ProcName, string TableName, IDataParameter[] Paras)
287
{
288
try
289
{
290
SqlCommand cmd = new SqlCommand();
291
cmd.Connection = conn;
292
cmd.CommandType = CommandType.StoredProcedure;
293
cmd.CommandText = ProcName;
294
295
if (Paras != null)
296
{
297
foreach (IDataParameter Para in Paras)
298
{
299
cmd.Parameters.Add(Para);
300
}
301
}
302
303
SqlDataAdapter da = new SqlDataAdapter(cmd);
304
DataSet ds = new DataSet();
305
da.Fill(ds, TableName);
306
return ds;
307
}
308
catch (System.Exception exp)
309
{
310
throw (new System.Exception("执行存储过程返回数据记录集错误:" + exp.Message));
311
}
312
}
313
#endregion
314
315
执行命令返回第一行第一列#region 执行命令返回第一行第一列
316
public object ExecuteCommandScalar(string CommandString, IDataParameter[] Paras)
317
{
318
SqlCommand cmd = new SqlCommand(CommandString, conn);
319
if (Paras != null)
320
{
321
foreach (IDataParameter Para in Paras)
322
{
323
cmd.Parameters.Add(Para);
324
}
325
}
326
object obj = null;
327
try
328
{
329
Open();
330
obj = cmd.ExecuteScalar();
331
return obj;
332
}
333
catch (System.Exception exp)
334
{
335
throw (new System.Exception("执行命令返回第一行第一列错误:" + exp.Message));
336
}
337
finally
338
{
339
Close();
340
}
341
}
342
#endregion
343
344
执行存储过程返回第一行第一列#region 执行存储过程返回第一行第一列
345
public object ExecuteProcScalar(string ProcName, IDataParameter[] Paras)
346
{
347
try
348
{
349
SqlCommand cmd = new SqlCommand();
350
cmd.Connection = conn;
351
cmd.CommandType = CommandType.StoredProcedure;
352
cmd.CommandText = ProcName;
353
354
if (Paras != null)
355
{
356
foreach (IDataParameter Para in Paras)
357
{
358
cmd.Parameters.Add(Para);
359
}
360
}
361
362
object obj = null;
363
obj = GetScalar(cmd);
364
return obj;
365
}
366
catch (System.Exception exp)
367
{
368
throw (new System.Exception("执行存储过程返回第一行第一列错误:" + exp.Message));
369
}
370
}
371
#endregion
372
373
执行命令返回IDataReader#region 执行命令返回IDataReader
374
public IDataReader GetDataReaderFromExcuteCommand(string CommandString, IDataParameter[] Paras)
375
{
376
try
377
{
378
SqlCommand cmd = new SqlCommand(CommandString, conn);
379
if (Paras != null)
380
{
381
foreach (IDataParameter Para in Paras)
382
{
383
cmd.Parameters.Add(Para);
384
}
385
}
386
387
SqlDataReader Reader = null;
388
389
Open();
390
Reader = cmd.ExecuteReader();
391
return Reader;
392
}
393
catch (System.Exception exp)
394
{
395
throw (new System.Exception("执行命令返回数据读取器错误:" + exp.Message));
396
}
397
finally
398
{
399
Close();
400
}
401
}
402
#endregion
403
404
执行存储过程返回IDataReader#region 执行存储过程返回IDataReader
405
public IDataReader GetDataReaderFromExcuteProc(string ProcName, IDataParameter[] Paras)
406
{
407
try
408
{
409
SqlCommand cmd = new SqlCommand();
410
cmd.Connection = conn;
411
cmd.CommandType = CommandType.StoredProcedure;
412
cmd.CommandText = ProcName;
413
if (Paras != null)
414
{
415
foreach (IDataParameter Para in Paras)
416
{
417
cmd.Parameters.Add(Para);
418
}
419
}
420
421
SqlDataReader Reader = null;
422
423
Open();
424
Reader = cmd.ExecuteReader();
425
return Reader;
426
}
427
catch (System.Exception exp)
428
{
429
throw (new System.Exception("执行存储过程返回数据读取器错误:" + exp.Message));
430
}
431
finally
432
{
433
Close();
434
}
435
}
436
#endregion
437
438
#endregion
439
}
440
#endregion
441
442
OLEDB类#region OLEDB类
443
public class OledbAccess : IDataAccess
444
{
445
IDataAccess 成员#region IDataAccess 成员
446
447
private string strConn;
448
private OleDbConnection conn;
449
450
无参数构造#region 无参数构造
451
public OledbAccess()
452
{
453
strConn = "";
454
conn = new OleDbConnection();
455
}
456
#endregion
457
458
有参数构造#region 有参数构造
459
public OledbAccess(string ConnectionString)
460
{
461
strConn = ConnectionString;
462
conn = new OleDbConnection(strConn);
463
}
464
#endregion
465
466
返回第一行第一列方法#region 返回第一行第一列方法
467
public object GetScalar(OleDbCommand cmd)
468
{
469
object obj = null;
470
try
471
{
472
Open();
473
obj = cmd.ExecuteScalar();
474
}
475
catch (Exception exp)
476
{
477
throw (new Exception("获取第一行第一列数据错误:" + exp.Message));
478
}
479
finally
480
{
481
Close();
482
}
483
return obj;
484
}
485
#endregion
486
487
数据库连接字符串属性器#region 数据库连接字符串属性器
488
public string ConnectionString
489
{
490
get
491
{
492
return strConn;
493
}
494
set
495
{
496
strConn = value;
497
conn.ConnectionString = strConn;
498
}
499
}
500
#endregion
501
502
打开数据库#region 打开数据库
503
public void Open()
504
{
505
try
506
{
507
if (conn.State != ConnectionState.Open) // 判断数据库连接状态是否打开
508
{
509
conn.Open(); // 打开数据库连接
510
}
511
}
512
catch (Exception exp)
513
{
514
throw (new Exception("打开数据库连接错误:" + exp.Message));
515
}
516
}
517
#endregion
518
519
关闭数据库#region 关闭数据库
520
public void Close()
521
{
522
try
523
{
524
if (conn.State != ConnectionState.Closed) // 判断数据库连接是否关闭
525
{
526
conn.Close(); // 关闭数据库连接
527
}
528
}
529
catch (Exception exp)
530
{
531
throw (new Exception("关闭数据库连接错误:" + exp.Message));
532
}
533
}
534
#endregion
535
536
执行命令#region 执行命令
537
public void ExcuteCommand(string CommandString, IDataParameter[] Paras)
538
{
539
try
540
{
541
OleDbCommand cmd = new OleDbCommand(CommandString, conn);
542
if (Paras != null)
543
{
544
foreach (IDataParameter Para in Paras)
545
{
546
cmd.Parameters.Add(Para);
547
}
548
}
549
Open();
550
cmd.ExecuteNonQuery();
551
}
552
catch (System.Exception exp)
553
{
554
throw (new System.Exception("执行命令错误:" + exp.Message));
555
}
556
finally
557
{
558
Close();
559
}
560
}
561
#endregion
562
563
执行命令返回DataSet#region 执行命令返回DataSet
564
public DataSet GetDataSetFromExcuteCommand(string CommandString, string TableName, IDataParameter[] Paras)
565
{
566
try
567
{
568
OleDbCommand cmd = new OleDbCommand(CommandString, conn);
569
if (Paras != null)
570
{
571
foreach (IDataParameter Para in Paras)
572
{
573
cmd.Parameters.Add(Para);
574
}
575
}
576
577
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
578
DataSet ds = new DataSet();
579
da.Fill(ds, TableName);
580
return ds;
581
}
582
catch (System.Exception exp)
583
{
584
throw (new System.Exception("执行命令返回DataSet:" + exp.Message));
585
}
586
}
587
#endregion
588
589
执行存储过程#region 执行存储过程
590
public void ExecuteProc(string ProcName, IDataParameter[] Paras)
591
{
592
try
593
{
594
OleDbCommand cmd = new OleDbCommand();
595
cmd.Connection = conn;
596
cmd.CommandType = CommandType.StoredProcedure;
597
cmd.CommandText = ProcName;
598
599
if (Paras != null)
600
{
601
foreach (IDataParameter Para in Paras)
602
{
603
cmd.Parameters.Add(Para);
604
}
605
}
606
607
Open();
608
cmd.ExecuteNonQuery();
609
}
610
catch (System.Exception exp)
611
{
612
throw (new System.Exception("执行存储过程错误:" + exp.Message));
613
}
614
finally
615
{
616
Close();
617
}
618
}
619
#endregion
620
621
执行存储过程返回DataSet#region 执行存储过程返回DataSet
622
public DataSet GetDataSetFromExcuteProc(string ProcName, string TableName, IDataParameter[] Paras)
623
{
624
try
625
{
626
OleDbCommand cmd = new OleDbCommand();
627
cmd.Connection = conn;
628
cmd.CommandType = CommandType.StoredProcedure;
629
cmd.CommandText = ProcName;
630
631
if (Paras != null)
632
{
633
foreach (IDataParameter Para in Paras)
634
{
635
cmd.Parameters.Add(Para);
636
}
637
}
638
639
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
640
DataSet ds = new DataSet();
641
da.Fill(ds, TableName);
642
return ds;
643
}
644
catch (System.Exception exp)
645
{
646
throw (new System.Exception("执行存储过程返回数据记录集错误:" + exp.Message));
647
}
648
}
649
#endregion
650
651
执行命令返回第一行第一列#region 执行命令返回第一行第一列
652
public object ExecuteCommandScalar(string CommandString, IDataParameter[] Paras)
653
{
654
OleDbCommand cmd = new OleDbCommand(CommandString, conn);
655
if (Paras != null)
656
{
657
foreach (IDataParameter Para in Paras)
658
{
659
cmd.Parameters.Add(Para);
660
}
661
}
662
object obj = null;
663
try
664
{
665
Open();
666
obj = cmd.ExecuteScalar();
667
return obj;
668
}
669
catch (System.Exception exp)
670
{
671
throw (new System.Exception("执行命令返回第一行第一列错误:" + exp.Message));
672
}
673
finally
674
{
675
Close();
676
}
677
}
678
#endregion
679
680
执行存储过程返回第一行第一列#region 执行存储过程返回第一行第一列
681
public object ExecuteProcScalar(string ProcName, IDataParameter[] Paras)
682
{
683
try
684
{
685
OleDbCommand cmd = new OleDbCommand();
686
cmd.Connection = conn;
687
cmd.CommandType = CommandType.StoredProcedure;
688
cmd.CommandText = ProcName;
689
690
if (Paras != null)
691
{
692
foreach (IDataParameter Para in Paras)
693
{
694
cmd.Parameters.Add(Para);
695
}
696
}
697
698
object obj = null;
699
obj = GetScalar(cmd);
700
return obj;
701
}
702
catch (System.Exception exp)
703
{
704
throw (new System.Exception("执行存储过程返回第一行第一列错误:" + exp.Message));
705
}
706
}
707
#endregion
708
709
执行命令返回IDataReader#region 执行命令返回IDataReader
710
public IDataReader GetDataReaderFromExcuteCommand(string CommandString, IDataParameter[] Paras)
711
{
712
try
713
{
714
OleDbCommand cmd = new OleDbCommand(CommandString, conn);
715
if (Paras != null)
716
{
717
foreach (IDataParameter Para in Paras)
718
{
719
cmd.Parameters.Add(Para);
720
}
721
}
722
723
OleDbDataReader Reader = null;
724
725
Open();
726
Reader = cmd.ExecuteReader();
727
return Reader;
728
}
729
catch (System.Exception exp)
730
{
731
throw (new System.Exception("执行命令返回数据读取器错误:" + exp.Message));
732
}
733
finally
734
{
735
Close();
736
}
737
}
738
#endregion
739
740
执行存储过程返回IDataReader#region 执行存储过程返回IDataReader
741
public IDataReader GetDataReaderFromExcuteProc(string ProcName, IDataParameter[] Paras)
742
{
743
try
744
{
745
OleDbCommand cmd = new OleDbCommand();
746
cmd.Connection = conn;
747
cmd.CommandType = CommandType.StoredProcedure;
748
cmd.CommandText = ProcName;
749
if (Paras != null)
750
{
751
foreach (IDataParameter Para in Paras)
752
{
753
cmd.Parameters.Add(Para);
754
}
755
}
756
757
OleDbDataReader Reader = null;
758
759
Open();
760
Reader = cmd.ExecuteReader();
761
return Reader;
762
}
763
catch (System.Exception exp)
764
{
765
throw (new System.Exception("执行存储过程返回数据读取器错误:" + exp.Message));
766
}
767
finally
768
{
769
Close();
770
}
771
}
772
#endregion
773
774
#endregion
775
}
776
#endregion
777
}

2

3

4

5

6

7

8

9



10


11

12



13

14

15

16

17


18

19



20


21

22



23

24

25



26

27



28

29

30

31

32

33

34

35

36

37



38

39

40



41

42



43

44

45

46

47

48

49

50

51

52


53

54



55

56

57



58

59



60

61

62

63

64

65

66

67

68



69

70

71



72

73



74

75

76

77

78

79

80

81

82

83

84


85

86



87

88



89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106


107

108



109


110

111

112

113

114


115

116



117

118

119

120

121

122


123

124



125

126

127

128

129

130


131

132



133

134

135



136

137

138

139

140



141

142

143

144



145

146

147

148

149

150

151


152

153



154

155



156

157

158

159



160

161

162

163

164

165

166


167

168



169

170



171

172



173

174

175

176

177



178

179

180

181

182

183


184

185



186

187



188

189



190

191

192

193

194



195

196

197

198

199

200


201

202



203

204



205

206

207



208

209



210

211

212

213

214

215

216

217



218

219

220

221



222

223

224

225

226

227


228

229



230

231



232

233

234



235

236



237

238

239

240

241

242

243

244

245

246

247



248

249

250

251

252

253


254

255



256

257



258

259

260

261

262

263

264



265

266



267

268

269

270

271

272

273

274

275



276

277

278

279



280

281

282

283

284

285


286

287



288

289



290

291

292

293

294

295

296



297

298



299

300

301

302

303

304

305

306

307

308

309



310

311

312

313

314

315


316

317



318

319

320



321

322



323

324

325

326

327

328



329

330

331

332

333

334



335

336

337

338



339

340

341

342

343

344


345

346



347

348



349

350

351

352

353

354

355



356

357



358

359

360

361

362

363

364

365

366

367



368

369

370

371

372

373


374

375



376

377



378

379

380



381

382



383

384

385

386

387

388

389

390

391

392

393

394



395

396

397

398



399

400

401

402

403

404


405

406



407

408



409

410

411

412

413

414



415

416



417

418

419

420

421

422

423

424

425

426

427

428



429

430

431

432



433

434

435

436

437

438

439

440

441

442


443

444



445


446

447

448

449

450


451

452



453

454

455

456

457

458


459

460



461

462

463

464

465

466


467

468



469

470

471



472

473

474

475

476



477

478

479

480



481

482

483

484

485

486

487


488

489



490

491



492

493

494

495



496

497

498

499

500

501

502


503

504



505

506



507

508



509

510

511

512

513



514

515

516

517

518

519


520

521



522

523



524

525



526

527

528

529

530



531

532

533

534

535

536


537

538



539

540



541

542

543



544

545



546

547

548

549

550

551

552

553



554

555

556

557



558

559

560

561

562

563


564

565



566

567



568

569

570



571

572



573

574

575

576

577

578

579

580

581

582

583



584

585

586

587

588

589


590

591



592

593



594

595

596

597

598

599

600



601

602



603

604

605

606

607

608

609

610

611



612

613

614

615



616

617

618

619

620

621


622

623



624

625



626

627

628

629

630

631

632



633

634



635

636

637

638

639

640

641

642

643

644

645



646

647

648

649

650

651


652

653



654

655

656



657

658



659

660

661

662

663

664



665

666

667

668

669

670



671

672

673

674



675

676

677

678

679

680


681

682



683

684



685

686

687

688

689

690

691



692

693



694

695

696

697

698

699

700

701

702

703



704

705

706

707

708

709


710

711



712

713



714

715

716



717

718



719

720

721

722

723

724

725

726

727

728

729

730



731

732

733

734



735

736

737

738

739

740


741

742



743

744



745

746

747

748

749

750



751

752



753

754

755

756

757

758

759

760

761

762

763

764



765

766

767

768



769

770

771

772

773

774

775

776

777
