温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:缤纷企业管理系统源码
当前文件:
BinFenEnterpriseWeb/App_Code/DBFun.cs[20K,2009-6-12 11:33:52],打开代码结构图
BinFenEnterpriseWeb/App_Code/DBFun.cs[20K,2009-6-12 11:33:52],打开代码结构图1using System; 2
using System.Data; 3
using System.Configuration; 4
using System.Web; 5
using System.Web.Security; 6
using System.Web.UI; 7
using System.Web.UI.WebControls; 8
using System.Web.UI.WebControls.WebParts; 9
using System.Web.UI.HtmlControls; 10
using System.Text.RegularExpressions; 11
using System.Data.OleDb; 12
//该源码下载自www.51aspx.com(51aspx.com) 13
14
/// <summary> 15
/// WebFunction 的摘要说明 16
/// </summary> 17
public class DBFun 18
{ 19
protected static OleDbConnection conn = new OleDbConnection(); 20
protected static OleDbCommand comm = new OleDbCommand(); 21
22
public DBFun() 23
{ 24
// 25
// TODO: 在此处添加构造函数逻辑 26
// 27
} 28
29
/// <summary> 30
/// 打开数据库连接 31
/// </summary> 32
//51_a_s_p_x.c_o_m 33
34
private static void openConnection() 35
{ 36
if (conn.State == ConnectionState.Closed ) 37
{ 38
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" 39
+ System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.ConnectionStrings["DbPath"].ToString()); 40
comm.Connection = conn; 41
try 42
{ 43
conn.Open(); 44
45
} 46
catch (Exception e) 47
{ 48
throw new Exception(e.Message); 49
} 50
} 51
} 52
/// <summary> 53
/// 关闭当前数据库连接 54
/// </summary> 55
private static void closeConnection() 56
{ 57
if (conn.State == ConnectionState.Open) 58
conn.Close(); 59
conn.Dispose(); 60
comm.Dispose(); 61
} 62
/// <summary> 63
/// 执行Sql查询语句 64
/// </summary> 65
/// <param name="sqlstr">传入的Sql语句</param> 66
public static void ExecuteSql(string sqlstr) 67
{ 68
try 69
{ 70
openConnection(); 71
comm.CommandType = CommandType.Text; 72
comm.CommandText = sqlstr; 73
comm.ExecuteNonQuery(); 74
} 75
catch (Exception e) 76
{ 77
throw new Exception(e.Message); 78
} 79
finally 80
{ 81
closeConnection(); 82
} 83
} 84
85
/// <summary> 86
/// 执行Sql更新语句 87
/// </summary> 88
/// <param name="sqlstr">传入的Sql语句</param> 89
/// <returns>布尔值</returns> 90
public static bool ExecuteUpdate(string sqlstr) 91
{ 92
int isUpdateOk = 0; 93
try 94
{ 95
openConnection(); 96
comm.CommandType = CommandType.Text; 97
comm.CommandText = sqlstr; 98
isUpdateOk = Convert.ToInt32(comm.ExecuteNonQuery()); 99
} 100
catch (Exception e) 101
{ 102
throw new Exception(e.Message); 103
} 104
finally 105
{ 106
closeConnection(); 107
} 108
if (isUpdateOk > 0) 109
{ 110
return true; 111
} 112
else 113
{ 114
return false; 115
} 116
} 117
118
119
public static DataRow GetDataRow(string strSQL) 120
{ 121
//查询数据,取得数据行 122
try 123
{ 124
openConnection(); 125
OleDbDataAdapter OleAdp = new OleDbDataAdapter(strSQL, conn); 126
DataSet Rs = new DataSet(); 127
OleAdp.Fill(Rs); 128
if (Rs.Tables[0].Rows.Count != 0) 129
return Rs.Tables[0].Rows[0]; 130
else 131
return null; 132
} 133
catch 134
{ 135
return null; 136
} 137
138
} 139
140
public static DataView GetDataView(string strSQL) 141
{ 142
//查询数据,取得数据视图 143
try 144
{ 145
openConnection(); 146
OleDbDataAdapter OleAdp = new OleDbDataAdapter(strSQL, conn); 147
DataSet Rs = new DataSet(); 148
OleAdp.Fill(Rs); 149
return Rs.Tables[0].DefaultView; 150
151
} 152
catch (Exception e) 153
{ 154
throw new Exception(e.Message); 155
} 156
157
} 158
public static bool SearchTable(string strSQL) 159
{ 160
OleDbDataReader OleDr = null; 161
try 162
{ 163
openConnection(); 164
comm.CommandText = strSQL; 165
comm.CommandType = CommandType.Text; 166
OleDr = comm.ExecuteReader(CommandBehavior.CloseConnection); 167
if (OleDr.Read()) 168
{ 169
170
return true; 171
} 172
else 173
{ 174
175
return false; 176
} 177
} 178
catch 179
{ 180
try 181
{ 182
OleDr.Close(); 183
closeConnection(); 184
} 185
catch 186
{ 187
} 188
return false; 189
} 190
finally 191
{ 192
if (OleDr != null) 193
OleDr.Close(); 194
} 195
196
} 197
public static string SearchValue(string strSQL) 198
{ 199
//查找 200
OleDbDataReader OleDr = null; 201
try 202
{ 203
openConnection(); 204
comm.CommandText = strSQL; 205
comm.CommandType = CommandType.Text; 206
OleDr = comm.ExecuteReader(CommandBehavior.CloseConnection); 207
if (OleDr.Read()) 208
{ 209
210
return OleDr[0].ToString(); 211
} 212
else 213
return ""; 214
} 215
catch 216
{ 217
try 218
{ 219
OleDr.Close(); 220
closeConnection(); 221
} 222
catch 223
{ 224
} 225
return ""; 226
} 227
finally 228
{ 229
if (OleDr!=null) 230
OleDr.Close(); 231
} 232
233
} 234
public static void FillDwList(DropDownList ddlist, string strqry) 235
{ 236
//用SQL语句填充下拉列表 237
try 238
{ 239
DataView dv = GetDataView(strqry); 240
ddlist.DataValueField = dv.Table.Columns[0].Caption.ToString(); 241
ddlist.DataTextField = dv.Table.Columns[1].Caption.ToString(); 242
ddlist.DataSource = dv; 243
ddlist.DataBind(); 244
} 245
catch (Exception ex) 246
{ 247
throw new Exception(ex.Message); 248
} 249
} 250
251
/// <summary> 252
/// 执行Sql查询语句并返回第一行的第一条记录,返回值为object 使用时需要拆箱操作 -> Unbox 253
/// </summary> 254
/// <param name="sqlstr">传入的Sql语句</param> 255
/// <returns>object 返回值 </returns> 256
public static object ExecuteScalar(string sqlstr) 257
{ 258
object obj = new object(); 259
try 260
{ 261
openConnection(); 262
comm.CommandType = CommandType.Text; 263
comm.CommandText = sqlstr; 264
obj = comm.ExecuteScalar(); 265
} 266
catch (Exception e) 267
{ 268
throw new Exception(e.Message); 269
} 270
finally 271
{ 272
closeConnection(); 273
} 274
return obj; 275
} 276
277
/// <summary> 278
/// 返回指定Sql语句的SqlDataReader,请注意,在使用后请关闭本对象,同时将自动调用closeConnection()来关闭数据库连接 279
/// 方法关闭数据库连接 280
/// </summary> 281
/// <param name="sqlstr">传入的Sql语句</param> 282
/// <returns>SqlDataReader对象</returns> 283
public static OleDbDataReader dataReader(string sqlstr) 284
{ 285
OleDbDataReader dr = null; 286
try 287
{ 288
openConnection(); 289
comm.CommandText = sqlstr; 290
comm.CommandType = CommandType.Text; 291
dr = comm.ExecuteReader(CommandBehavior.CloseConnection); 292
} 293
catch 294
{ 295
try 296
{ 297
dr.Close(); 298
closeConnection(); 299
} 300
catch 301
{ 302
} 303
} 304
return dr; 305
} 306
307
///<summary> 308
///关闭datareader 309
///传入SqlDataReader的ref 310
///</summary> 311
public static void closeDataReader(ref OleDbDataReader sdr) 312
{ 313
try 314
{ 315
sdr.Close(); 316
sdr.Dispose(); 317
} 318
catch (Exception) 319
{ } 320
} 321
322
/// <summary> 323
/// 返回指定Sql语句的DataSet 324
/// </summary> 325
/// <param name="sqlstr">传入的Sql语句</param> 326
/// <returns>DataSet</returns> 327
public static DataSet dataSet(string sqlstr) 328
{ 329
DataSet ds = new DataSet(); 330
OleDbDataAdapter da = new OleDbDataAdapter(); 331
try 332
{ 333
openConnection(); 334
comm.CommandType = CommandType.Text; 335
comm.CommandText = sqlstr; 336
da.SelectCommand = comm; 337
da.Fill(ds); 338
} 339
catch (Exception e) 340
{ 341
throw new Exception(e.Message); 342
} 343
finally 344
{ 345
closeConnection(); 346
} 347
return ds; 348
} 349
350
/// <summary> 351
/// 返回指定Sql语句的DataSet 352
/// </summary> 353
/// <param name="sqlstr">传入的Sql语句</param> 354
/// <param name="ds">传入的引用DataSet对象</param> 355
public static void dataSet(string sqlstr, ref DataSet ds) 356
{ 357
OleDbDataAdapter da = new OleDbDataAdapter(); 358
try 359
{ 360
openConnection(); 361
comm.CommandType = CommandType.Text; 362
comm.CommandText = sqlstr; 363
da.SelectCommand = comm; 364
da.Fill(ds); 365
} 366
catch (Exception e) 367
{ 368
throw new Exception(e.Message); 369
} 370
371
finally 372
{ 373
closeConnection(); 374
} 375
} 376
/// <summary> 377
/// 返回指定Sql语句的DataTable 378
/// </summary> 379
/// <param name="sqlstr">传入的Sql语句</param> 380
/// <returns>DataTable</returns> 381
public static DataTable dataTable(string sqlstr) 382
{ 383
OleDbDataAdapter da = new OleDbDataAdapter(); 384
DataTable datatable = new DataTable(); 385
try 386
{ 387
openConnection(); 388
comm.CommandType = CommandType.Text; 389
comm.CommandText = sqlstr; 390
da.SelectCommand = comm; 391
da.Fill(datatable); 392
} 393
catch (Exception) 394
{ 395
} 396
finally 397
{ 398
closeConnection(); 399
} 400
return datatable; 401
} 402
403
/// <summary> 404
/// 执行指定Sql语句,同时给传入DataTable进行赋值 405
/// </summary> 406
/// <param name="sqlstr">传入的Sql语句</param> 407
/// <param name="dt">ref DataTable dt </param> 408
public static void dataTable(string sqlstr, ref DataTable dt) 409
{ 410
OleDbDataAdapter da = new OleDbDataAdapter(); 411
try 412
{ 413
openConnection(); 414
comm.CommandType = CommandType.Text; 415
comm.CommandText = sqlstr; 416
da.SelectCommand = comm; 417
da.Fill(dt); 418
} 419
catch (Exception e) 420
{ 421
throw new Exception(e.Message); 422
} 423
finally 424
{ 425
closeConnection(); 426
} 427
} 428
429
/// <summary> 430
/// 执行指定Sql语句 431
/// </summary> 432
/// <param name="sqlstr">传入的Sql语句</param> 433
public static DataView dataView(string sqlstr) 434
{ 435
OleDbDataAdapter da = new OleDbDataAdapter(); 436
DataView dv = new DataView(); 437
DataSet ds = new DataSet(); 438
try 439
{ 440
openConnection(); 441
comm.CommandType = CommandType.Text; 442
comm.CommandText = sqlstr; 443
da.SelectCommand = comm; 444
da.Fill(ds); 445
dv = ds.Tables[0].DefaultView; 446
} 447
catch (Exception e) 448
{ 449
throw new Exception(e.Message); 450
} 451
finally 452
{ 453
closeConnection(); 454
} 455
return dv; 456
} 457
458
/// <summary> 459
/// 执行指定Sql语句,开始记录位置,和返回记录数 460
/// </summary> 461
/// <param name="sqlstr">传入的Sql语句</param> 462
/// <param name="StartIndex">开始记录位置</param> 463
/// <param name="pageSize">返回记录数</param> 464
public static DataView dataView(string sqlstr, int StartIndex, int pageSize) 465
{ 466
OleDbDataAdapter da = new OleDbDataAdapter(); 467
DataView dv = new DataView(); 468
DataSet ds = new DataSet(); 469
try 470
{ 471
openConnection(); 472
comm.CommandType = CommandType.Text; 473
comm.CommandText = sqlstr; 474
da.SelectCommand = comm; 475
da.Fill(ds, StartIndex, pageSize, "pagelist"); 476
dv = ds.Tables["pagelist"].DefaultView; 477
} 478
catch (Exception e) 479
{ 480
throw new Exception(e.Message); 481
} 482
finally 483
{ 484
closeConnection(); 485
} 486
return dv; 487
} 488
489
public static string cutTitle(string str, int len) 490
{ 491
int intLen = str.Length; 492
int start = 0; 493
int end = intLen; 494
int single = 0; 495
char[] chars = str.ToCharArray(); 496
for (int i = 0; i < chars.Length; i++) 497
{ 498
if (Convert.ToInt32(chars[i]) > 255) 499
{ 500
start += 2; 501
} 502
else 503
{ 504
start += 1; 505
single++; 506
} 507
if (start >= len) 508
{ 509
510
if (end % 2 == 0) 511
{ 512
if (single % 2 == 0) 513
{ 514
end = i + 1; 515
} 516
else 517
{ 518
end = i; 519
} 520
} 521
else 522
{ 523
end = i + 1; 524
} 525
break; 526
} 527
} 528
string temp = str.Substring(0, end); 529
if (str.Length > end) 530
{ 531
return temp + "..."; 532
} 533
else 534
{ 535
return temp; 536
} 537
} 538
539
/// <summary> 540
/// 验证是否正确 541
/// </summary> 542
/// <param name="userName">传入sql语句</param> 543
/// <returns>布尔值</returns> 544
public static bool chkExist(string sqlstr) 545
{ 546
int isExit = 0; 547
try 548
{ 549
openConnection(); 550
comm.CommandType = CommandType.Text; 551
comm.CommandText = sqlstr; 552
isExit = Convert.ToInt32(comm.ExecuteScalar()); 553
} 554
catch (Exception e) 555
{ 556
throw new Exception(e.Message); 557
} 558
finally 559
{ 560
closeConnection(); 561
} 562
if (isExit > 0) 563
{ 564
return true; 565
} 566
else 567
{ 568
return false; 569
} 570
} 571
572
public static string safeForm(string str) 573
{ 574
str = str.Trim(); 575
str = str.Replace("'", "''"); 576
return str; 577
} 578
579
public static string alertChk(string str, int how) 580
{ 581
switch (how) 582
{ 583
case 1: 584
return "<script>alert('" + str + "');window.location.href='Default.aspx'</script>"; 585
case 2: 586
return "<script>alert('" + str + "');window.close()</script>"; 587
default: 588
return "<script>alert('" + str + "');history.go(-1)</script>"; 589
} 590
} 591
592
/// <summary> 593
/// 替换html中的特殊字符 594
/// </summary> 595
/// <param name="theString">需要进行替换的文本。</param> 596
/// <returns>替换完的文本。</returns> 597
public static string HtmlEncode(string theString) 598
{ 599
theString = theString.Replace(">", ">"); 600
theString = theString.Replace("<", "<"); 601
theString = theString.Replace(" ", " "); 602
theString = theString.Replace(" ", " "); 603
theString = theString.Replace("\"", """); 604
theString = theString.Replace("\'", "'"); 605
theString = theString.Replace("\n", "<br/> "); 606
return theString; 607
} 608
609
/// <summary> 610
/// 恢复html中的特殊字符 611
/// </summary> 612
/// <param name="theString">需要恢复的文本。</param> 613
/// <returns>恢复好的文本。</returns> 614
public static string HtmlDiscode(string theString) 615
{ 616
theString = theString.Replace(">", ">"); 617
theString = theString.Replace("<", "<"); 618
theString = theString.Replace(" ", " "); 619
theString = theString.Replace(" ", " "); 620
theString = theString.Replace(""", "\""); 621
theString = theString.Replace("'", "\'"); 622
theString = theString.Replace("<br/> ", "\n"); 623
return theString; 624
} 625
626
public static string DealHtml(string str) 627
{ 628
str = Regex.Replace(str, @"\<(img)[^>]*>|<\/(img)>", "", RegexOptions.IgnoreCase); 629
str = Regex.Replace(str, @"\<(table|tbody|tr|td|th|)[^>]*>|<\/(table|tbody|tr|td|th|)>", "", RegexOptions.IgnoreCase); 630
str = Regex.Replace(str, @"\<(div|blockquote|fieldset|legend)[^>]*>|<\/(div|blockquote|fieldset|legend)>", "", RegexOptions.IgnoreCase); 631
str = Regex.Replace(str, @"\<(font|i|u|h[1-9]|s)[^>]*>|<\/(font|i|u|h[1-9]|s)>", "", RegexOptions.IgnoreCase); 632
str = Regex.Replace(str, @"\<(style|strong)[^>]*>|<\/(style|strong)>", "", RegexOptions.IgnoreCase); 633
str = Regex.Replace(str, @"\<a[^>]*>|<\/a>", "", RegexOptions.IgnoreCase); 634
str = Regex.Replace(str, @"\<(meta|iframe|frame|span|tbody|layer)[^>]*>|<\/(iframe|frame|meta|span|tbody|layer)>", "", RegexOptions.IgnoreCase); 635
str = Regex.Replace(str, @"\<a[^>]*", "", RegexOptions.IgnoreCase); 636
return str; 637
} 638
639
public static string PageList(int size, string funClick, int countNum, int currentPage) 640
{ 641
int pageCount = 0; 642
int stepNum = 3; 643
int pageRoot = 1; 644
string pageStr = ""; 645
int pageSize = size; 646
if (countNum % pageSize == 0) 647
{ 648
pageCount = countNum / pageSize; 649
} 650
else 651
{ 652
pageCount = countNum / pageSize + 1; 653
} 654
pageStr = "<div id='pager'>分页:" + currentPage.ToString() + "/" + pageCount.ToString() + "页"; 655
if (currentPage - stepNum > 1) 656
{ 657
pageRoot = currentPage - stepNum; 658
} 659
int pageFoot = pageCount; 660
if (currentPage + stepNum < pageCount) 661
{ 662
pageFoot = currentPage + stepNum; 663
} 664
if (pageRoot == 1) 665
{ 666
if (currentPage == 1) 667
{ 668
pageStr += "<font color=888888 face=webdings>9</font></a>"; 669
pageStr += "<font color=888888 face=webdings>7</font></a> "; 670
} 671
else 672
{ 673
pageStr += "<a href='#page.1' onclick='" + funClick + "(1)' title='首页'><font face=webdings>9</font></a>"; 674
pageStr += "<a href='#page." + Convert.ToString(currentPage - 1) + "' onclick='" + funClick + "(" + Convert.ToString(currentPage - 1) + ")' title='上页'><font face=webdings>7</font></a> "; 675
} 676
} 677
else 678
{ 679
pageStr += "<a href='#page.1' onclick='" + funClick + "(1)' title='首页'><font face=webdings>9</font></a>"; 680
pageStr += "<a href='#page." + Convert.ToString(currentPage - 1) + "' onclick='" + funClick + "(" + Convert.ToString(currentPage - 1) + ")' title='上页'><font face=webdings>7</font></a>..."; 681
} 682
for (int i = pageRoot; i <= pageFoot; i++) 683
{ 684
if (i == currentPage) 685
{ 686
pageStr += "<font color='red'>[" + i.ToString() + "]</font> "; 687
} 688
else 689
{ 690
pageStr += "<a href='#page." + i.ToString() + "' onclick='" + funClick + "(" + i.ToString() + ")'>[" + i.ToString() + "]</a> "; 691
} 692
if (i == pageCount) 693
break; 694
} 695
if (pageFoot == pageCount) 696
{ 697
if (pageCount == currentPage) 698
{ 699
pageStr += "<font color=888888 face=webdings>8</font></a>"; 700
pageStr += "<font color=888888 face=webdings>:</font></a>"; 701
} 702
else 703
{ 704
pageStr += "<a href='#page." + Convert.ToString(currentPage + 1) + "' onclick='" + funClick + "(" + Convert.ToString(currentPage + 1) + ")' title='下页'><font face=webdings>8</font></a>"; 705
pageStr += "<a href='#page." + pageCount.ToString() + "' onclick='" + funClick + "(" + pageCount.ToString() + ")' title='尾页'><font face=webdings>:</font></a>"; 706
} 707
} 708
else 709
{ 710
pageStr += "... <a href='#page." + Convert.ToString(currentPage + 1) + "' onclick='" + funClick + "(" + Convert.ToString(currentPage + 1) + ")' title='下页'><font face=webdings>8</font></a>"; 711
pageStr += "<a href='#page." + pageCount.ToString() + "' onclick='" + funClick + "(" + pageCount.ToString() + ")' title='尾页'><font face=webdings>:</font></a>"; 712
} 713
pageStr += "</div>"; 714
return pageStr; 715
} 716
} 717









