发个盗版[c#操作数据库类].
1

/**//// <summary>2
/// 数据库类3
/// </summary>4
public class Db4SQL5

{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

13

/**//// <summary>14
/// 15
/// </summary>16
public Db4SQL()17

{18
this.lConnectionKeepOpenTf = false;19
this.lTransactionCount = 0;20
this.lConnectionStr = Global.Static.SQLDbConnectionStr;21
}22

23

/**//// <summary>24
/// 把两个SqlConnection连接关闭lConnection,lConnectionWithTransaction,同时重置连接字符串lConnectionStr=Global.Static.SQLDbConnectionStr25
/// </summary>26
public void ConnectionClear()27

{28
ConnectionStrInit();29
if ((this.lConnection != null) && (this.lConnection.State == ConnectionState.Open))30

{31
this.lConnection.Close();32
this.lConnection = null;33
}34
if ((this.lConnectionWithTransaction != null) && (this.lConnectionWithTransaction.State == ConnectionState.Open))35

{36
this.lConnectionWithTransaction.Close();37
this.lConnectionWithTransaction = null;38
}39
}40

41

/**//// <summary>42
/// 得到一个SqlConnection对象43
/// </summary>44
/// <param name="pInTransactionTf">是否开事务</param>45
/// <returns></returns>46
internal SqlConnection ConnectionGet(bool pInTransactionTf)47

{48
if (pInTransactionTf)49

{50
if (this.lConnectionWithTransaction == null)51

{52
throw new Exception("TransactionBegin 未执行");53
}54
return this.lConnectionWithTransaction;55
}56
if (this.lConnectionKeepOpenTf)57

{58
if (this.lConnection == null)59

{60
this.lConnection = new SqlConnection(this.lConnectionStr);61
}62
if (this.lConnection.State != ConnectionState.Open)63

{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

73

/**//// <summary>74
/// 得到一个DataTable75
/// </summary>76
/// <param name="pSqlStr">要执行的字符串</param>77
/// <returns></returns>78
public System.Data.DataTable DataTableGet(string pSqlStr)79

{80
return this.DataTableGet(pSqlStr, false);81
}82

83

/**//// <summary>84
/// 得到一个DataTable85
/// </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

{91
SqlConnection connection = this.ConnectionGet(pInTransactionTf);92
SqlCommand command = new SqlCommand(pSqlStr, connection);93
if (pInTransactionTf)94

{95
command.Transaction = this.lTransaction;96
}97
command.CommandTimeout = 0x2710;//1000098
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
try104

{105
adapter.Fill(dataTable);106
}107
catch (System.Exception exception)108

{109
throw new Exception(exception.Message+" 执行(" + pSqlStr + ")时出错");110
}111
finally112

{113
if (!pInTransactionTf && !this.lConnectionKeepOpenTf)114

{115
connection.Close();116
}117
}118
return dataTable;119
}120

121

/**//// <summary>122
/// 执行一条语句123
/// </summary>124
/// <param name="pSqlString"></param>125
/// <returns>返回影响条数</returns>126
public int Execute(string pSqlString)127

{128
return this.Execute(pSqlString, false);129
}130

131

/**//// <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

{139
SqlConnection connection = this.ConnectionGet(pInTransactionTf);140
SqlCommand command = new SqlCommand(pSqlString, connection);141
command.CommandTimeout = 0x2710;142
if (pInTransactionTf)143

{144
command.Transaction = this.lTransaction;145
}146
try147

{148
return command.ExecuteNonQuery();149
}150
catch (System.Exception exception)151

{152
throw new Exception(exception.Message + "执行(" + pSqlString + ")时出错");153
}154
finally155

{156
if (!pInTransactionTf && !this.lConnectionKeepOpenTf)157

{158
connection.Close();159
}160
}161
return 0;162
}163

164

/**//// <summary>165
/// 得到 0行0列 的值166
/// </summary>167
/// <param name="pSqlString"></param>168
/// <returns></returns>169
public object FindAField(string pSqlString)170

{171
return this.FindAField(pSqlString, false);172
}173

174

/**//// <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

{182
SqlCommand command = new SqlCommand(pSqlString, this.ConnectionGet(pInTransactionTf));183
command.CommandTimeout = 0x2710;//10000184
if (pInTransactionTf)185

{186
command.Transaction = this.lTransaction;187
try188

{189
return command.ExecuteScalar();190
}191
catch (System.Exception exception)192

{193
throw new Exception(exception.Message + "执行(" + pSqlString + ")时出错");194
}195
}196
else if (!this.lConnectionKeepOpenTf)197

{198
try199

{200
return command.ExecuteScalar();201
}202
catch (System.Exception exception2)203

{204
throw new Exception(exception2.Message+"执行(" + pSqlString + ")时出错");205
}206
finally207

{208
command.Connection.Close();209
}210
}211
else212

{213
try214

{215
return command.ExecuteScalar();216
}217
catch (System.Exception exception3)218

{219
throw new Exception(exception3.Message+"执行(" + pSqlString + ")时出错");220
}221
}222
return null;223
}224

225

/**//// <summary>226
/// 是否存在以pTableID为名称的表227
/// </summary>228
/// <param name="pTableID"></param>229
/// <returns></returns>230
public bool TableExistsTf(string pTableID)231

{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

236

/**//// <summary>237
/// 开始事务238
/// </summary>239
public void TransactionBegin()240

{241
if (this.lTransactionCount == 0)242

{243
this.lConnectionWithTransaction = new SqlConnection(this.ConnectionStr);244
this.lConnectionWithTransaction.Open();245
this.lTransaction = this.lConnectionWithTransaction.BeginTransaction();246
}247
this.lTransactionCount++;248
}249

250

/**//// <summary>251
/// 提交事务252
/// </summary>253
public void TransactionCommit()254

{255
if (this.lTransactionCount == 0)256

{257
throw new Exception("TransactionBegin未执行");258
}259
this.lTransactionCount--;260
if (this.lTransactionCount == 0)261

{262
this.lTransaction.Commit();263
this.lConnectionWithTransaction.Close();264
this.lConnectionWithTransaction = null;265
}266
}267

268

/**//// <summary>269
/// 回滚事务270
/// </summary>271
public void TransactionRollback()272

{273
if (this.lTransactionCount == 0)274

{275
throw new Exception("TransactionBegin未执行");276
}277
this.lTransactionCount--;278
if (this.lTransactionCount == 0)279

{280
this.lTransaction.Rollback();281
this.lConnectionWithTransaction.Close();282
this.lConnectionWithTransaction = null;283
}284
}285

286

/**//// <summary>287
/// 是否存在某视图288
/// </summary>289
/// <param name="pViewName"></param>290
/// <returns></returns>291
public bool ViewExistsTf(string pViewName)292

{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

297

298

/**//// <summary>299
/// 是否把连接保持Open300
/// </summary>301
public bool ConnectionKeepOpenTf302

{303
get304

{305
return this.lConnectionKeepOpenTf;306
}307
set308

{309
this.lConnectionKeepOpenTf = value;310
if ((!this.lConnectionKeepOpenTf && (this.lConnection != null)) && (this.lConnection.State == ConnectionState.Open))311

{312
this.lConnection.Close();313
}314
}315
}316

317

/**//// <summary>318
/// 连接字符串319
/// </summary>320
public string ConnectionStr321

{322
get323

{324
return this.lConnectionStr;325
}326
}327

328

/**//// <summary>329
/// 初始化连接字符串lConnectionStr330
/// </summary>331
private void ConnectionStrInit()332

{333
this.lConnectionStr = Global.Static.SQLDbConnectionStr;334
}335

336

/**//// <summary>337
/// 事务338
/// </summary>339
internal SqlTransaction Transaction340

{341
get342

{343
return this.lTransaction;344
}345
}346
}347

348

349

350

351

352

/**//// <summary>353
/// 数据库类354
/// </summary>355
public class Db4ACCESS356

{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

364

/**//// <summary>365
/// 366
/// </summary>367
public Db4ACCESS()368

{369
this.lConnectionKeepOpenTf = false;370
this.lTransactionCount = 0;371
this.lConnectionStr = Global.Static.ACCESSDbConnectionStr;372
}373

374

/**//// <summary>375
/// 把两个Connection连接关闭lConnection,lConnectionWithTransaction,同时重置连接字符串lConnectionStr=Global.Static.ACCESSDbConnectionStr376
/// </summary>377
public void ConnectionClear()378

{379
ConnectionStrInit();380

381
if ((this.lConnection != null) && (this.lConnection.State == ConnectionState.Open))382

{383
this.lConnection.Close();384
this.lConnection = null;385
}386
if ((this.lConnectionWithTransaction != null) && (this.lConnectionWithTransaction.State == ConnectionState.Open))387

{388
this.lConnectionWithTransaction.Close();389
this.lConnectionWithTransaction = null;390
}391
}392

393

/**//// <summary>394
/// 得到一个Connection对象395
/// </summary>396
/// <param name="pInTransactionTf">是否开事务</param>397
/// <returns></returns>398
internal OleDbConnection ConnectionGet(bool pInTransactionTf)399

{400
if (pInTransactionTf)401

{402
if (this.lConnectionWithTransaction == null)403

{404
throw new Exception("TransactionBegin 未执行");405
}406
return this.lConnectionWithTransaction;407
}408
if (this.lConnectionKeepOpenTf)409

{410
if (this.lConnection == null)411

{412
this.lConnection = new OleDbConnection(this.lConnectionStr);413
}414
if (this.lConnection.State != ConnectionState.Open)415

{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

425

/**//// <summary>426
/// 得到一个DataTable427
/// </summary>428
/// <param name="pSqlStr">要执行的字符串</param>429
/// <returns></returns>430
public System.Data.DataTable DataTableGet(string pSqlStr)431

{432
return this.DataTableGet(pSqlStr, false);433
}434

435

/**//// <summary>436
/// 得到一个DataTable437
/// </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

{443
OleDbConnection connection = this.ConnectionGet(pInTransactionTf);444
OleDbCommand command = new OleDbCommand(pSqlStr, connection);445
if (pInTransactionTf)446

{447
command.Transaction = this.lTransaction;448
}449
command.CommandTimeout = 0x2710;//10000450
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
try456

{457
adapter.Fill(dataTable);458
}459
catch (System.Exception exception)460

{461
throw new Exception(exception.Message+" 执行(" + pSqlStr + ")时出错");462
}463
finally464

{465
if (!pInTransactionTf && !this.lConnectionKeepOpenTf)466

{467
connection.Close();468
}469
}470
return dataTable;471
}472

473

/**//// <summary>474
/// 执行一条语句475
/// </summary>476
/// <param name="pSqlString"></param>477
/// <returns>返回影响条数</returns>478
public int Execute(string pSqlString)479

{480
return this.Execute(pSqlString, false);481
}482

483

/**//// <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

{491
OleDbConnection connection = this.ConnectionGet(pInTransactionTf);492
OleDbCommand command = new OleDbCommand(pSqlString, connection);493
command.CommandTimeout = 0x2710;494
if (pInTransactionTf)495

{496
command.Transaction = this.lTransaction;497
}498
try499

{500
return command.ExecuteNonQuery();501
}502
catch (System.Exception exception)503

{504
throw new Exception(exception.Message + "执行(" + pSqlString + ")时出错");505
}506
finally507

{508
if (!pInTransactionTf && !this.lConnectionKeepOpenTf)509

{510
connection.Close();511
}512
}513
return 0;514
}515

516

/**//// <summary>517
/// 得到 0行0列 的值518
/// </summary>519
/// <param name="pSqlString"></param>520
/// <returns></returns>521
public object FindAField(string pSqlString)522

{523
return this.FindAField(pSqlString, false);524
}525

526

/**//// <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

{534
OleDbCommand command = new OleDbCommand(pSqlString, this.ConnectionGet(pInTransactionTf));535
command.CommandTimeout = 0x2710;//10000536
if (pInTransactionTf)537

{538
command.Transaction = this.lTransaction;539
try540

{541
return command.ExecuteScalar();542
}543
catch (System.Exception exception)544

{545
throw new Exception(exception.Message + "执行(" + pSqlString + ")时出错");546
}547
}548
else if (!this.lConnectionKeepOpenTf)549

{550
try551

{552
return command.ExecuteScalar();553
}554
catch (System.Exception exception2)555

{556
throw new Exception(exception2.Message+"执行(" + pSqlString + ")时出错");557
}558
finally559

{560
command.Connection.Close();561
}562
}563
else564

{565
try566

{567
return command.ExecuteScalar();568
}569
catch (System.Exception exception3)570

{571
throw new Exception(exception3.Message+"执行(" + pSqlString + ")时出错");572
}573
}574
return null;575
}576

577

/**//// <summary>578
/// 是否存在以pTableID为名称的表579
/// </summary>580
/// <param name="pTableID"></param>581
/// <returns></returns>582
public bool TableExistsTf(string pTableID)583

{584
string pSqlString = "select count(*) from "+pTableID;585
try586

{587
this.Execute(pSqlString);588
}589
catch590

{591
return false;592
}593
return true;594
}595

596

/**//// <summary>597
/// 开始事务598
/// </summary>599
public void TransactionBegin()600

{601
if (this.lTransactionCount == 0)602

{603
this.lConnectionWithTransaction = new OleDbConnection(this.ConnectionStr);604
this.lConnectionWithTransaction.Open();605
this.lTransaction = this.lConnectionWithTransaction.BeginTransaction();606
}607
this.lTransactionCount++;608
}609

610

/**//// <summary>611
/// 提交事务612
/// </summary>613
public void TransactionCommit()614

{615
if (this.lTransactionCount == 0)616

{617
throw new Exception("TransactionBegin未执行");618
}619
this.lTransactionCount--;620
if (this.lTransactionCount == 0)621

{622
this.lTransaction.Commit();623
this.lConnectionWithTransaction.Close();624
this.lConnectionWithTransaction = null;625
}626
}627

628

/**//// <summary>629
/// 回滚事务630
/// </summary>631
public void TransactionRollback()632

{633
if (this.lTransactionCount == 0)634

{635
throw new Exception("TransactionBegin未执行");636
}637
this.lTransactionCount--;638
if (this.lTransactionCount == 0)639

{640
this.lTransaction.Rollback();641
this.lConnectionWithTransaction.Close();642
this.lConnectionWithTransaction = null;643
}644
}645

646

647

/**//// <summary>648
/// 是否把连接保持Open649
/// </summary>650
public bool ConnectionKeepOpenTf651

{652
get653

{654
return this.lConnectionKeepOpenTf;655
}656
set657

{658
this.lConnectionKeepOpenTf = value;659
if ((!this.lConnectionKeepOpenTf && (this.lConnection != null)) && (this.lConnection.State == ConnectionState.Open))660

{661
this.lConnection.Close();662
}663
}664
}665

666

/**//// <summary>667
/// 连接字符串668
/// </summary>669
public string ConnectionStr670

{671
get672

{673
return this.lConnectionStr;674
}675
}676

677

/**//// <summary>678
/// 初始化连接字符串lConnectionStr679
/// </summary>680
private void ConnectionStrInit()681

{682
this.lConnectionStr = Global.Static.ACCESSDbConnectionStr;683
}684

685

/**//// <summary>686
/// 事务687
/// </summary>688
internal OleDbTransaction Transaction689

{690
get691

{692
return this.lTransaction;693
}694
}695

696
}697

1

/**//// <summary>2
/// Global 的摘要说明。3
/// </summary>4
public class Static5

{6
private Static()7

{8
//9
// TODO: 在此处添加构造函数逻辑10
//11
}12

13

/**//// <summary>14
/// such as: server=127.0.0.1;database=tt;uid=sa;pwd=;15
/// </summary>16
public static string SQLDbConnectionStr="";17

/**//// <summary>18
/// such as: PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=dataBasePath;Password=pwd;19
/// </summary>20
public static string ACCESSDbConnectionStr="";21
}
浙公网安备 33010602011771号