温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:品杰电子商务购物平台系统源码
当前文件:
PinJieShop/Classes/Base.cs[9K,2009-6-12 11:52:01],打开代码结构图
PinJieShop/Classes/Base.cs[9K,2009-6-12 11:52:01],打开代码结构图1using System; 2
using System.Data; 3
using System.Data.SqlClient; 4
using System.Data.OleDb; 5
using System.Configuration; 6
//51-A-s-p-x.com 7
8
namespace shop.DbBase 9
...{ 10
/**//// <summary> 11
/// SqlBase. 12
/// </summary> 13
public class SqlBase 14
...{ 15
16
"Fields of SqlBase calss"#region "Fields of SqlBase calss" 17
18
/**//// <summary> 19
/// connecting to Database 20
/// </summary> 21
protected static string sConn = ConfigurationSettings.AppSettings["ConnectionString"]; 22
23
#endregion 24
25
"Functions of SqlBase class"#region "Functions of SqlBase class" 26
public SqlBase() 27
...{ 28
// 29
// TODO: Add constructor logic here 30
// 31
} 32
33
/**//// <summary> 34
/// executing SQL commands 35
/// </summary> 36
/// <param name="strSQL">string</param> 37
/// <returns>return int</returns> 38
public static int ExecuteSql(string strSQL) 39
...{ 40
SqlConnection myCn = new SqlConnection(sConn); 41
SqlCommand myCmd = new SqlCommand(strSQL,myCn); 42
try 43
...{ 44
myCn.Open(); 45
myCmd.ExecuteNonQuery(); 46
return 0; 47
} 48
catch(System.Data.SqlClient.SqlException e) 49
...{ 50
throw new Exception(e.Message); 51
} 52
finally 53
...{ 54
myCmd.Dispose(); 55
myCn.Close(); 56
} 57
} 58
59
60
/**//// <summary> 61
///executing SQL commands 62
/// </summary> 63
/// <param name="strSQL">要执行的SQL语句,为字符串类型string</param> 64
/// <returns>返回执行情况,整形int</returns> 65
public static int ExecuteSqlEx(string strSQL) 66
...{ 67
SqlConnection myCn = new SqlConnection(sConn); 68
SqlCommand myCmd = new SqlCommand(strSQL,myCn); 69
70
try 71
...{ 72
myCn.Open(); 73
SqlDataReader myReader = myCmd.ExecuteReader(); 74
if(myReader.Read()) 75
...{ 76
return 0; 77
} 78
else 79
...{ 80
throw new Exception("Value Unavailable!"); 81
} 82
} 83
catch(System.Data.SqlClient.SqlException e) 84
...{ 85
throw new Exception(e.Message); 86
} 87
finally 88
...{ 89
myCmd.Dispose(); 90
myCn.Close(); 91
} 92
} 93
94
95
/**//// <summary> 96
/// get dataset 97
/// </summary> 98
/// <param name="strSQL">(string)</param> 99
/// <returns>(DataSet)</returns> 100
public static DataSet ExecuteSql4Ds(string strSQL) 101
...{ 102
SqlConnection myCn = new SqlConnection(sConn); 103
try 104
...{ 105
myCn.Open(); 106
SqlDataAdapter sda = new SqlDataAdapter(strSQL,myCn); 107
DataSet ds = new DataSet("ds"); 108
sda.Fill(ds); 109
return ds; 110
} 111
catch(System.Data.SqlClient.SqlException e) 112
...{ 113
throw new Exception(e.Message); 114
} 115
finally 116
...{ 117
myCn.Close(); 118
} 119
} 120
121
122
/**//// <summary> 123
/// get single value 124
/// </summary> 125
/// <param name="strSQL">(string)</param> 126
/// <returns>(int)</returns> 127
public static int ExecuteSql4Value(string strSQL) 128
...{ 129
SqlConnection myCn = new SqlConnection(sConn); 130
SqlCommand myCmd = new SqlCommand(strSQL,myCn); 131
try 132
...{ 133
myCn.Open(); 134
object r = myCmd.ExecuteScalar(); 135
if(Object.Equals(r,null)) 136
...{ 137
throw new Exception("value unavailable!"); 138
} 139
else 140
...{ 141
return (int)r; 142
} 143
} 144
catch(System.Data.SqlClient.SqlException e) 145
...{ 146
throw new Exception(e.Message); 147
} 148
finally 149
...{ 150
myCmd.Dispose(); 151
myCn.Close(); 152
} 153
} 154
155
156
/**//// <summary> 157
/// get object 158
/// </summary> 159
/// <param name="strSQL">(string)</param> 160
/// <returns>(object)</returns> 161
public static object ExecuteSql4ValueEx(string strSQL) 162
...{ 163
SqlConnection myCn = new SqlConnection(sConn); 164
SqlCommand myCmd = new SqlCommand(strSQL,myCn); 165
try 166
...{ 167
myCn.Open(); 168
object r = myCmd.ExecuteScalar(); 169
if(Object.Equals(r,null)) 170
...{ 171
throw new Exception("object unavailable!"); 172
} 173
else 174
...{ 175
return r; 176
} 177
} 178
catch(System.Data.SqlClient.SqlException e) 179
...{ 180
throw new Exception(e.Message); 181
} 182
finally 183
...{ 184
myCmd.Dispose(); 185
myCn.Close(); 186
} 187
} 188
189
190
/**//// <summary> 191
/// execute multipul SQL commands 192
/// </summary> 193
/// <param name="strSQLs">string</param> 194
/// <returns>int</returns> 195
public static int ExecuteSqls(string[] strSQLs) 196
...{ 197
SqlConnection myCn = new SqlConnection(sConn); 198
SqlCommand myCmd = new SqlCommand(); 199
int j=strSQLs.Length; 200
201
try 202
...{ 203
myCn.Open(); 204
} 205
catch(System.Data.SqlClient.SqlException e) 206
...{ 207
throw new Exception(e.Message); 208
} 209
SqlTransaction myTrans = myCn.BeginTransaction(); 210
211
try 212
...{ 213
myCmd.Connection = myCn; 214
myCmd.Transaction = myTrans; 215
216
foreach(string str in strSQLs) 217
...{ 218
myCmd.CommandText = str; 219
myCmd.ExecuteNonQuery(); 220
} 221
myTrans.Commit(); 222
return 0; 223
} 224
catch(System.Data.SqlClient.SqlException e) 225
...{ 226
myTrans.Rollback(); 227
throw new Exception(e.Message); 228
} 229
finally 230
...{ 231
myCmd.Dispose(); 232
myCn.Close(); 233
} 234
} 235
236
#endregion 237
} 238
239
240
241
/**//// <summary> 242
/// OleBase. 243
/// </summary> 244
public class OleBase 245
...{ 246
247
"Fields of OleBase calss"#region "Fields of OleBase calss" 248
249
/**//// <summary> 250
/// connecting to Database 251
/// </summary> 252
protected static string strConn = ConfigurationSettings.AppSettings["ConnectionString"]; 253
254
#endregion 255
256
"Functions of OleBase class"#region "Functions of OleBase class" 257
public OleBase() 258
...{ 259
// 260
// TODO: Add constructor logic here 261
// 262
} 263
264
/**//// <summary> 265
/// executing SQL commands 266
/// </summary> 267
/// <param name="strSQL">string</param> 268
/// <returns>return int</returns> 269
public static int ExecuteSql(string strSQL) 270
...{ 271
OleDbConnection myCn = new OleDbConnection(strConn); 272
OleDbCommand myCmd = new OleDbCommand(strSQL,myCn); 273
try 274
...{ 275
myCn.Open(); 276
myCmd.ExecuteNonQuery(); 277
return 0; 278
} 279
catch(OleDbException e) 280
...{ 281
throw new Exception(e.Message/**//* + " at databse :" + strConn*/); 282
} 283
finally 284
...{ 285
myCmd.Dispose(); 286
myCn.Close(); 287
} 288
} 289
290
291
/**//// <summary> 292
///executing SQL commands 293
/// </summary> 294
/// <param name="strSQL">要执行的SQL语句,为字符串类型string</param> 295
/// <returns>返回执行情况,整形int</returns> 296
public static int ExecuteSqlEx(string strSQL) 297
...{ 298
OleDbConnection myCn = new OleDbConnection(strConn); 299
OleDbCommand myCmd = new OleDbCommand(strSQL,myCn); 300
301
try 302
...{ 303
myCn.Open(); 304
OleDbDataReader myReader = myCmd.ExecuteReader(); 305
if(myReader.Read()) 306
...{ 307
return 0; 308
} 309
else 310
...{ 311
throw new Exception("Value Unavailable!"); 312
} 313
} 314
catch(OleDbException e) 315
...{ 316
throw new Exception(e.Message); 317
} 318
finally 319
...{ 320
myCmd.Dispose(); 321
myCn.Close(); 322
} 323
} 324
325
326
/**//// <summary> 327
/// get dataset 328
/// </summary> 329
/// <param name="strSQL">(string)</param> 330
/// <returns>(DataSet)</returns> 331
public static DataSet ExecuteSql4Ds(string strSQL) 332
...{ 333
OleDbConnection myCn = new OleDbConnection(strConn); 334
try 335
...{ 336
myCn.Open(); 337
OleDbDataAdapter sda = new OleDbDataAdapter(strSQL,myCn); 338
DataSet ds = new DataSet("ds"); 339
sda.Fill(ds); 340
return ds; 341
} 342
catch(OleDbException e) 343
...{ 344
throw new Exception(e.Message); 345
} 346
finally 347
...{ 348
myCn.Close(); 349
} 350
} 351
352
353
/**//// <summary> 354
/// get single value 355
/// </summary> 356
/// <param name="strSQL">(string)</param> 357
/// <returns>(int)</returns> 358
public static int ExecuteSql4Value(string strSQL) 359
...{ 360
OleDbConnection myCn = new OleDbConnection(strConn); 361
OleDbCommand myCmd = new OleDbCommand(strSQL,myCn); 362
try 363
...{ 364
myCn.Open(); 365
object r = myCmd.ExecuteScalar(); 366
if(Object.Equals(r,null)) 367
...{ 368
throw new Exception("value unavailable!"); 369
} 370
else 371
...{ 372
return (int)r; 373
} 374
} 375
catch(OleDbException e) 376
...{ 377
throw new Exception(e.Message); 378
} 379
finally 380
...{ 381
myCmd.Dispose(); 382
myCn.Close(); 383
} 384
} 385
386
387
/**//// <summary> 388
/// get object 389
/// </summary> 390
/// <param name="strSQL">(string)</param> 391
/// <returns>(object)</returns> 392
public static object ExecuteSql4ValueEx(string strSQL) 393
...{ 394
OleDbConnection myCn = new OleDbConnection(strConn); 395
OleDbCommand myCmd = new OleDbCommand(strSQL,myCn); 396
try 397
...{ 398
myCn.Open(); 399
object r = myCmd.ExecuteScalar(); 400
if(Object.Equals(r,null)) 401
...{ 402
throw new Exception("object unavailable!"); 403
} 404
else 405
...{ 406
return r; 407
} 408
} 409
catch(OleDbException e) 410
...{ 411
throw new Exception(e.Message); 412
} 413
finally 414
...{ 415
myCmd.Dispose(); 416
myCn.Close(); 417
} 418
} 419
420
421
/**//// <summary> 422
/// execute multipul SQL commands 423
/// </summary> 424
/// <param name="strSQLs">string</param> 425
/// <returns>int</returns> 426
public static int ExecuteSqls(string[] strSQLs) 427
...{ 428
OleDbConnection myCn = new OleDbConnection(strConn); 429
OleDbCommand myCmd = new OleDbCommand(); 430
int j=strSQLs.Length; 431
432
try 433
...{ 434
myCn.Open(); 435
} 436
catch(OleDbException e) 437
...{ 438
throw new Exception(e.Message); 439
} 440
OleDbTransaction myTrans = myCn.BeginTransaction(); 441
442
try 443
...{ 444
myCmd.Connection = myCn; 445
myCmd.Transaction = myTrans; 446
447
foreach(string str in strSQLs) 448
...{ 449
myCmd.CommandText = str; 450
myCmd.ExecuteNonQuery(); 451
} 452
myTrans.Commit(); 453
return 0; 454
} 455
catch(OleDbException e) 456
...{ 457
myTrans.Rollback(); 458
throw new Exception(e.Message); 459
} 460
finally 461
...{ 462
myCmd.Dispose(); 463
myCn.Close(); 464
} 465
} 466
467
#endregion 468
} 469
} 470






}