标签:
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Data; 5 6 using System.Configuration; 7 using System.Data.Common; 8 using System.Data.SqlClient; 9 10 using Microsoft.Practices.EnterpriseLibrary.Data; 11 using Microsoft.Practices.EnterpriseLibrary.Data.Sql; 12 using Microsoft.Practices.EnterpriseLibrary.Data.Configuration; 13 using Microsoft.Practices.EnterpriseLibrary.Data.Instrumentation; 14 using Microsoft.Practices.EnterpriseLibrary.Data.Properties; 15 using Microsoft.Practices.EnterpriseLibrary.Common; 16 using System.Security.Cryptography; 17 using System.IO; 18 using System.Windows.Forms; 19 20 21 namespace BO 22 { 23 public class SqlHelperExtensionLocal 24 { 25 26 public const int PREORDAIN_TYPE = 9; 27 public static string BaseStoreID = ""; 28 public static string BaseTimeBar = ""; 29 public static string BaseCloseStoreTime = "05:00:00"; 30 31 /// <summary> 32 /// 服务器脱网标志 33 /// </summary> 34 public static bool OffLine = true; 35 /// <summary> 36 /// 脱网数据标志 37 /// </summary> 38 public static bool OffDataFlag = false; 39 /// <summary> 40 /// 数据库服务器Ip地址 41 /// </summary> 42 public static string DataSourceIp1 = GetSocketIP(); 43 44 /// <summary> 45 /// 数据库服务器端口 46 /// </summary> 47 public static string DataSourcePort = GetDataSourcePort(); 48 /// <summary> 49 /// Socket发送端口 50 /// </summary> 51 public static string SocketSendPort = GetSocketPort(); 52 private const String CONFIG_CONNECTION_STRING = "DBConnectionStringLocal"; 53 private static SqlDatabase DATABASE; 54 private static DbTransaction DBTRANSACTION; 55 private static String DB_CONN_STRING = GetConnectionString(); 56 private static String CONNECT_FLAG = GetConnectFlag(); 57 58 //密钥 59 private const string KEY = "GERRARD8"; 60 //执行SQL的超时时间 61 private static int DBCOMMAND_TIMEOUT = GetTimeOut(); 62 63 //基本操作 64 #region "BasicProcess" 65 private static String GetConnectionString() 66 { 67 String connectionString; 68 try 69 { 70 //临时不加密 71 //connectionString = Decrypt(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString, KEY); 72 connectionString = ConfigurationManager.ConnectionStrings["DBConnectionStringLocal"].ConnectionString; 73 //connectionString = ConfigurationManager.ConnectionStrings[CONFIG_CONNECTION_STRING].ConnectionString; 74 } 75 catch (Exception e) 76 { 77 throw new Exception("连接数据库失败", e); 78 } 79 return connectionString; 80 } 81 82 private static String GetConnectFlag() 83 { 84 String strConnectFlag = ConfigurationManager.AppSettings["CONNECT_FLG"]; 85 if (strConnectFlag == null) 86 { 87 throw new Exception("读取连接类型失败"); 88 } 89 return strConnectFlag; 90 } 91 92 private static int GetTimeOut() 93 { 94 string timeout = ConfigurationManager.AppSettings["DBCOMMAND_TIMEOUT"].ToString(); 95 if (timeout == "") 96 { 97 throw new Exception("读取Timeout时间失败"); 98 } 99 return Convert.ToInt32(timeout); 100 } 101 private static String GetSocketIP() 102 { 103 DataSourceIp1 = ConfigurationManager.AppSettings["DataSourceIP1"]; 104 if (DataSourceIp1 == null) 105 { 106 throw new Exception("读取服务器IP失败"); 107 } 108 return DataSourceIp1; 109 } 110 private static String GetDataSourcePort() 111 { 112 string dataSourcePort = ConfigurationManager.AppSettings["DataSourcePort"]; 113 if (dataSourcePort == null) 114 { 115 throw new Exception("读取服务器数据库端口失败"); 116 } 117 return dataSourcePort; 118 } 119 private static String GetSocketPort() 120 { 121 string socketPort = ConfigurationManager.AppSettings["SocketSendPort"]; 122 if (socketPort == null) 123 { 124 throw new Exception("读取数据发送端口失败"); 125 } 126 return socketPort; 127 } 128 129 public static SqlDatabase GetDatabase() 130 { 131 try 132 { 133 //0:一直连接 134 if (CONNECT_FLAG == "0") 135 { 136 //没有创建则重新创建 137 if (DATABASE == null) 138 { 139 //DATABASE = DatabaseFactory.CreateDatabase(CONFIG_CONNECTION_STRING) as SqlDatabase; 140 DATABASE = new SqlDatabase(DB_CONN_STRING); 141 } 142 return DATABASE; 143 } 144 else //1:每次创建新的DATABASE 145 { 146 //SqlDatabase newDatabase = DatabaseFactory.CreateDatabase(CONFIG_CONNECTION_STRING) as SqlDatabase; 147 SqlDatabase newDatabase = new SqlDatabase(DB_CONN_STRING); 148 return newDatabase; 149 } 150 } 151 catch (SqlException e) 152 { 153 throw GetApplicationException(e); 154 } 155 catch (Exception e) 156 { 157 throw e; 158 } 159 } 160 161 private static void CloseDatabase(SqlDatabase sqlDatabase) 162 { 163 if (CONNECT_FLAG == "1") 164 { 165 if (sqlDatabase != null) 166 { 167 sqlDatabase = null; 168 } 169 } 170 } 171 172 private static Exception GetApplicationException(SqlException e) 173 { 174 switch (e.Number) 175 { 176 case 1231: 177 return new ApplicationException("服务器连接超时", e); 178 case 2627: 179 return new ApplicationException("主键重复", e); 180 case 207: 181 return new ApplicationException("列不存在", e); 182 case 208: 183 return new ApplicationException("表不存在", e); 184 case 53: 185 return new ApplicationException("服务器连接失败", e); 186 187 } 188 return e; 189 } 190 191 /// <summary> 192 /// 设置Dbcommand的超时时间,单位秒 193 /// </summary> 194 /// <param name="dbCommand"></param> 195 /// <returns></returns> 196 private static DbCommand SetDbCommand(DbCommand dbCommand) 197 { 198 dbCommand.CommandTimeout = DBCOMMAND_TIMEOUT; 199 return dbCommand; 200 } 201 #endregion 202 203 //共享事务处理 204 #region "ShareTransaction" 205 /// <summary> 206 /// 创建连接并打开,同时开启事务,并且将事务句柄存储在静态变量中 207 /// </summary> 208 public static DbTransaction OpenTransaction() 209 { 210 try 211 { 212 DbConnection dbConnection = GetDatabase().CreateConnection(); 213 if (dbConnection.State != ConnectionState.Open) 214 { 215 dbConnection.Open(); 216 } 217 DBTRANSACTION = dbConnection.BeginTransaction(); 218 return DBTRANSACTION; 219 } 220 catch (Exception e) 221 { 222 throw new Exception("打开事务失败", e); 223 } 224 } 225 226 227 /// <summary> 228 /// 回滚事务 229 /// </summary> 230 public static void RollbackTransaction() 231 { 232 if (DBTRANSACTION == null) 233 { 234 throw new Exception("回滚事务失败"); 235 } 236 else 237 { 238 DBTRANSACTION.Rollback(); 239 CloseDatabase(DATABASE); 240 DBTRANSACTION.Dispose(); 241 DBTRANSACTION = null; 242 } 243 } 244 245 /// <summary> 246 /// 提交事务 247 /// </summary> 248 public static void CommitTransaction() 249 { 250 if (DBTRANSACTION == null) 251 { 252 throw new Exception("提交事务失败"); 253 } 254 else 255 { 256 DBTRANSACTION.Commit(); 257 CloseDatabase(DATABASE); 258 DBTRANSACTION.Dispose(); 259 DBTRANSACTION = null; 260 } 261 } 262 #endregion 263 264 //单独事务处理 265 #region "SingleTransaction" 266 /// <summary> 267 /// 创建连接并打开,同时开启事务,并返回事务,作为单独事务进行后续操作 268 /// </summary> 269 public static DbTransaction OpenSingleTransaction() 270 { 271 try 272 { 273 DbConnection dbConnection = GetDatabase().CreateConnection(); 274 if (dbConnection.State != ConnectionState.Open) 275 { 276 dbConnection.Open(); 277 } 278 return dbConnection.BeginTransaction(); 279 } 280 catch (Exception e) 281 { 282 throw new Exception("打开事务失败", e); 283 } 284 } 285 286 287 /// <summary> 288 /// 回滚事务 289 /// </summary> 290 public static void RollbackSingleTransaction(DbTransaction dbTransaction) 291 { 292 try 293 { 294 dbTransaction.Rollback(); 295 CloseDatabase(DATABASE); 296 dbTransaction.Dispose(); 297 dbTransaction = null; 298 } 299 catch (Exception e) 300 { 301 throw new Exception("回滚事务失败", e); 302 } 303 } 304 305 /// <summary> 306 /// 提交事务 307 /// </summary> 308 public static void CommitSingleTransaction(DbTransaction dbTransaction) 309 { 310 try 311 { 312 dbTransaction.Commit(); 313 CloseDatabase(DATABASE); 314 dbTransaction.Dispose(); 315 dbTransaction = null; 316 } 317 catch (Exception e) 318 { 319 throw new Exception("提交事务失败", e); 320 } 321 } 322 #endregion 323 324 //参数转换 325 #region "AddParaToCommand" 326 /// <summary> 327 /// 将传入的字典参数集转换为SQL参数集 328 /// </summary> 329 /// <param name="dbCommand"></param> 330 /// <param name="dictParameter"></param> 331 private static void AddParaToCommand(DbCommand dbCommand, Dictionary<String, Object> dictParameter) 332 { 333 foreach (KeyValuePair<String, Object> kvp in dictParameter) 334 { 335 dbCommand.Parameters.Add(new SqlParameter(kvp.Key, kvp.Value)); 336 } 337 } 338 339 /// <summary> 340 /// 将传入的字典参数集转换为SQL参数集,参数类型为传出参数 341 /// </summary> 342 /// <param name="dbCommand"></param> 343 /// <param name="dictOutParameter"></param> 344 private static void AddParaToCommandOutput(DbCommand dbCommand, Dictionary<String, Object> dictOutParameter) 345 { 346 foreach (KeyValuePair<String, Object> kvp in dictOutParameter) 347 { 348 SqlParameter sqlParameter = new SqlParameter(kvp.Key, kvp.Value); 349 sqlParameter.Direction =ParameterDirection.InputOutput; 350 dbCommand.Parameters.Add(sqlParameter); 351 } 352 } 353 354 /// <summary> 355 /// 将执行好的SQL参数集返回给传出字典参数集 356 /// </summary> 357 /// <param name="dictOutParameter"></param> 358 /// <param name="dbCommand"></param> 359 private static void AddCommandToPara(ref Dictionary<String, Object> dictOutParameter, DbCommand dbCommand) 360 { 361 Dictionary<String, Object> newDictOutParameter = new Dictionary<String, Object>(); 362 foreach (KeyValuePair<String, Object> kvp in dictOutParameter) 363 { 364 newDictOutParameter.Add(kvp.Key, dbCommand.Parameters[kvp.Key].Value); 365 } 366 367 dictOutParameter = newDictOutParameter; 368 } 369 #endregion 370 371 //执行返回Dataset的SQL文 372 #region "ExecuteDataset" 373 public static DataSet ExecuteDataset(String strSQL) 374 { 375 SqlDatabase sqlDatabase = GetDatabase(); 376 try 377 { 378 DbCommand dbCommand = SetDbCommand(sqlDatabase.GetSqlStringCommand(strSQL)); 379 if (DBTRANSACTION == null) 380 { 381 return sqlDatabase.ExecuteDataSet(dbCommand); 382 } 383 else 384 { 385 return sqlDatabase.ExecuteDataSet(dbCommand, DBTRANSACTION); 386 } 387 } 388 catch (SqlException e) 389 { 390 throw GetApplicationException(e); 391 } 392 catch (Exception e) 393 { 394 throw e; 395 } 396 finally 397 { 398 CloseDatabase(sqlDatabase); 399 } 400 } 401 402 public static DataSet ExecuteDataset(String strSQL, Dictionary<String, Object> dictParameter) 403 { 404 SqlDatabase sqlDatabase = GetDatabase(); 405 try 406 { 407 DbCommand dbCommand = SetDbCommand(sqlDatabase.GetSqlStringCommand(strSQL)); 408 AddParaToCommand(dbCommand, dictParameter); 409 if (DBTRANSACTION == null) 410 { 411 return sqlDatabase.ExecuteDataSet(dbCommand); 412 } 413 else 414 { 415 return sqlDatabase.ExecuteDataSet(dbCommand, DBTRANSACTION); 416 } 417 } 418 catch (SqlException e) 419 { 420 throw GetApplicationException(e); 421 } 422 catch (Exception e) 423 { 424 throw e; 425 } 426 finally 427 { 428 CloseDatabase(sqlDatabase); 429 } 430 } 431 #endregion 432 433 //执行无返回Dataset的SQL文 434 #region "ExecuteNoQuery" 435 public static int ExecuteNoQuery(String strSQL) 436 { 437 SqlDatabase sqlDatabase = GetDatabase(); 438 try 439 { 440 DbCommand dbCommand = SetDbCommand(sqlDatabase.GetSqlStringCommand(strSQL)); 441 if (DBTRANSACTION == null) 442 { 443 return sqlDatabase.ExecuteNonQuery(dbCommand); 444 } 445 else 446 { 447 return sqlDatabase.ExecuteNonQuery(dbCommand, DBTRANSACTION); 448 } 449 } 450 catch (SqlException e) 451 { 452 throw GetApplicationException(e); 453 } 454 catch (Exception e) 455 { 456 throw e; 457 } 458 finally 459 { 460 CloseDatabase(sqlDatabase); 461 } 462 } 463 464 public static int ExecuteNoQuery(String strSQL, Dictionary<String, Object> dictParameter) 465 { 466 SqlDatabase sqlDatabase = GetDatabase(); 467 try 468 { 469 DbCommand dbCommand = SetDbCommand(sqlDatabase.GetSqlStringCommand(strSQL)); 470 AddParaToCommand(dbCommand, dictParameter); 471 if (DBTRANSACTION == null) 472 { 473 return sqlDatabase.ExecuteNonQuery(dbCommand); 474 } 475 else 476 { 477 return sqlDatabase.ExecuteNonQuery(dbCommand, DBTRANSACTION); 478 } 479 } 480 catch (SqlException e) 481 { 482 throw GetApplicationException(e); 483 } 484 catch (Exception e) 485 { 486 throw e; 487 } 488 finally 489 { 490 CloseDatabase(sqlDatabase); 491 } 492 } 493 #endregion 494 495 //执行返回Dataset的存储过程 496 #region "ExecuteDatasetProc" 497 public static DataSet ExecuteDatasetProc(String strProcName) 498 { 499 SqlDatabase sqlDatabase = GetDatabase(); 500 try 501 { 502 DbCommand dbCommand = SetDbCommand(sqlDatabase.GetStoredProcCommand(strProcName)); 503 if (DBTRANSACTION == null) 504 { 505 return sqlDatabase.ExecuteDataSet(dbCommand); 506 } 507 else 508 { 509 return sqlDatabase.ExecuteDataSet(dbCommand, DBTRANSACTION); 510 } 511 } 512 catch (SqlException e) 513 { 514 throw GetApplicationException(e); 515 } 516 catch (Exception e) 517 { 518 throw e; 519 } 520 finally 521 { 522 CloseDatabase(sqlDatabase); 523 } 524 } 525 526 public static DataSet ExecuteDatasetProc(String strProcName, Dictionary<String, Object> dictParameter) 527 { 528 SqlDatabase sqlDatabase = GetDatabase(); 529 try 530 { 531 DbCommand dbCommand = SetDbCommand(sqlDatabase.GetStoredProcCommand(strProcName)); 532 AddParaToCommand(dbCommand, dictParameter); 533 if (DBTRANSACTION == null) 534 { 535 return sqlDatabase.ExecuteDataSet(dbCommand); 536 } 537 else 538 { 539 return sqlDatabase.ExecuteDataSet(dbCommand, DBTRANSACTION); 540 } 541 } 542 catch (SqlException e) 543 { 544 throw GetApplicationException(e); 545 } 546 catch (Exception e) 547 { 548 throw e; 549 } 550 finally 551 { 552 CloseDatabase(sqlDatabase); 553 } 554 } 555 556 public static DataSet ExecuteDatasetProc(String strProcName, Dictionary<String, Object> dictParameter, ref Dictionary<String, Object> dictOutParameter) 557 { 558 SqlDatabase sqlDatabase = GetDatabase(); 559 try 560 { 561 DataSet dataset; 562 DbCommand dbCommand = SetDbCommand(sqlDatabase.GetStoredProcCommand(strProcName)); 563 AddParaToCommand(dbCommand, dictParameter); 564 AddParaToCommandOutput(dbCommand, dictOutParameter); 565 if (DBTRANSACTION == null) 566 { 567 dataset = sqlDatabase.ExecuteDataSet(dbCommand); 568 } 569 else 570 { 571 dataset = sqlDatabase.ExecuteDataSet(dbCommand, DBTRANSACTION); 572 } 573 AddCommandToPara(ref dictOutParameter, dbCommand); 574 return dataset; 575 } 576 catch (SqlException e) 577 { 578 throw GetApplicationException(e); 579 } 580 catch (Exception e) 581 { 582 throw e; 583 } 584 finally 585 { 586 CloseDatabase(sqlDatabase); 587 } 588 } 589 #endregion 590 591 //执行无返回Dataset的存储过程 592 #region "ExecuteNoQueryProc" 593 public static int ExecuteNoQueryProc(String strProcName) 594 { 595 SqlDatabase sqlDatabase = GetDatabase(); 596 try 597 { 598 DbCommand dbCommand = SetDbCommand(sqlDatabase.GetStoredProcCommand(strProcName)); 599 if (DBTRANSACTION == null) 600 { 601 return sqlDatabase.ExecuteNonQuery(dbCommand); 602 } 603 else 604 { 605 return sqlDatabase.ExecuteNonQuery(dbCommand, DBTRANSACTION); 606 } 607 } 608 catch (SqlException e) 609 { 610 throw GetApplicationException(e); 611 } 612 catch (Exception e) 613 { 614 throw e; 615 } 616 finally 617 { 618 CloseDatabase(sqlDatabase); 619 } 620 } 621 622 public static int ExecuteNoQueryProc(String strProcName, Dictionary<String, Object> dictParameter) 623 { 624 SqlDatabase sqlDatabase = GetDatabase(); 625 try 626 { 627 DbCommand dbCommand = SetDbCommand(sqlDatabase.GetStoredProcCommand(strProcName)); 628 AddParaToCommand(dbCommand, dictParameter); 629 if (DBTRANSACTION == null) 630 { 631 return sqlDatabase.ExecuteNonQuery(dbCommand); 632 } 633 else 634 { 635 return sqlDatabase.ExecuteNonQuery(dbCommand, DBTRANSACTION); 636 } 637 } 638 catch (SqlException e) 639 { 640 throw GetApplicationException(e); 641 } 642 catch (Exception e) 643 { 644 throw e; 645 } 646 finally 647 { 648 CloseDatabase(sqlDatabase); 649 } 650 } 651 //will be deleted 652 //public static int ExecuteNoQueryProc(String strProcName, Dictionary<String, Object> dictParameter, String strReturnPara) 653 //{ 654 // SqlDatabase sqlDatabase = GetDatabase(); 655 // try 656 // { 657 // DbCommand dbCommand = SetDbCommand(sqlDatabase.GetStoredProcCommand(strProcName)); 658 // AddParaToCommand(dbCommand, dictParameter); 659 // dbCommand.Parameters.Add(new SqlParameter(strReturnPara, SqlDbType.Int)); 660 // dbCommand.Parameters[strReturnPara].Direction = ParameterDirection.Output; 661 // if (DBTRANSACTION == null) 662 // { 663 // sqlDatabase.ExecuteNonQuery(dbCommand); 664 // } 665 // else 666 // { 667 // sqlDatabase.ExecuteNonQuery(dbCommand, DBTRANSACTION); 668 // } 669 // return int.Parse(dbCommand.Parameters[@strReturnPara].Value.ToString()); 670 // } 671 // catch (SqlException e) 672 // { 673 // throw GetApplicationException(e); 674 // } 675 // catch (Exception e) 676 // { 677 // throw e; 678 // } 679 // finally 680 // { 681 // CloseDatabase(sqlDatabase); 682 // } 683 //} 684 685 public static int ExecuteNoQueryProc(String strProcName, Dictionary<String, Object> dictParameter, ref Dictionary<String, Object> dictOutParameter) 686 { 687 SqlDatabase sqlDatabase = GetDatabase(); 688 try 689 { 690 int intAffected; 691 DbCommand dbCommand = SetDbCommand(sqlDatabase.GetStoredProcCommand(strProcName)); 692 AddParaToCommand(dbCommand, dictParameter); 693 AddParaToCommandOutput(dbCommand, dictOutParameter); 694 if (DBTRANSACTION == null) 695 { 696 intAffected = sqlDatabase.ExecuteNonQuery(dbCommand); 697 } 698 else 699 { 700 intAffected = sqlDatabase.ExecuteNonQuery(dbCommand, DBTRANSACTION); 701 } 702 AddCommandToPara(ref dictOutParameter, dbCommand); 703 return intAffected; 704 } 705 catch (SqlException e) 706 { 707 throw GetApplicationException(e); 708 } 709 catch (Exception e) 710 { 711 throw e; 712 } 713 finally 714 { 715 CloseDatabase(sqlDatabase); 716 } 717 } 718 #endregion 719 720 //在单独的事务中执行返回Dataset的SQL文 721 #region "ExecuteTransDataset" 722 public static DataSet ExecuteTransDataset(String strSQL, DbTransaction dbTransaction) 723 { 724 SqlDatabase sqlDatabase = GetDatabase(); 725 try 726 { 727 DbCommand dbCommand = SetDbCommand(sqlDatabase.GetSqlStringCommand(strSQL)); 728 return sqlDatabase.ExecuteDataSet(dbCommand, dbTransaction); 729 } 730 catch (SqlException e) 731 { 732 throw GetApplicationException(e); 733 } 734 catch (Exception e) 735 { 736 throw e; 737 } 738 finally 739 { 740 //CloseDatabase(sqlDatabase); 741 } 742 } 743 744 public static DataSet ExecuteTransDataset(String strSQL, Dictionary<String, Object> dictParameter, DbTransaction dbTransaction) 745 { 746 SqlDatabase sqlDatabase = GetDatabase(); 747 try 748 { 749 DbCommand dbCommand = SetDbCommand(sqlDatabase.GetSqlStringCommand(strSQL)); 750 AddParaToCommand(dbCommand, dictParameter); 751 return sqlDatabase.ExecuteDataSet(dbCommand, dbTransaction); 752 } 753 catch (SqlException e) 754 { 755 throw GetApplicationException(e); 756 } 757 catch (Exception e) 758 { 759 throw e; 760 } 761 finally 762 { 763 //CloseDatabase(sqlDatabase); 764 } 765 } 766 #endregion 767 768 //在单独的事务中执行无返回Dataset的SQL文 769 #region "ExecuteTransNoQuery" 770 public static int ExecuteTransNoQuery(String strSQL, DbTransaction dbTransaction) 771 { 772 SqlDatabase sqlDatabase = GetDatabase(); 773 try 774 { 775 DbCommand dbCommand = SetDbCommand(sqlDatabase.GetSqlStringCommand(strSQL)); 776 return sqlDatabase.ExecuteNonQuery(dbCommand, dbTransaction); 777 } 778 catch (SqlException e) 779 { 780 throw GetApplicationException(e); 781 } 782 catch (Exception e) 783 { 784 throw e; 785 } 786 finally 787 { 788 //CloseDatabase(sqlDatabase); 789 } 790 } 791 792 public static int ExecuteTransNoQuery(String strSQL, Dictionary<String, Object> dictParameter, DbTransaction dbTransaction) 793 { 794 SqlDatabase sqlDatabase = GetDatabase(); 795 try 796 { 797 DbCommand dbCommand = SetDbCommand(sqlDatabase.GetSqlStringCommand(strSQL)); 798 AddParaToCommand(dbCommand, dictParameter); 799 return sqlDatabase.ExecuteNonQuery(dbCommand, dbTransaction); 800 } 801 catch (SqlException e) 802 { 803 throw GetApplicationException(e); 804 } 805 catch (Exception e) 806 { 807 throw e; 808 } 809 finally 810 { 811 //CloseDatabase(sqlDatabase); 812 } 813 } 814 #endregion 815 816 //在单独的事务中执行返回Dataset的存储过程 817 #region "ExecuteTransDatasetProc" 818 public static DataSet ExecuteTransDatasetProc(String strProcName, DbTransaction dbTransaction) 819 { 820 SqlDatabase sqlDatabase = GetDatabase(); 821 try 822 { 823 DbCommand dbCommand = SetDbCommand(sqlDatabase.GetStoredProcCommand(strProcName)); 824 return sqlDatabase.ExecuteDataSet(dbCommand, dbTransaction); 825 } 826 catch (SqlException e) 827 { 828 throw GetApplicationException(e); 829 } 830 catch (Exception e) 831 { 832 throw e; 833 } 834 finally 835 { 836 //CloseDatabase(sqlDatabase); 837 } 838 } 839 840 public static DataSet ExecuteTransDatasetProc(String strProcName, Dictionary<String, Object> dictParameter, DbTransaction dbTransaction) 841 { 842 SqlDatabase sqlDatabase = GetDatabase(); 843 try 844 { 845 DbCommand dbCommand = SetDbCommand(sqlDatabase.GetStoredProcCommand(strProcName)); 846 AddParaToCommand(dbCommand, dictParameter); 847 return sqlDatabase.ExecuteDataSet(dbCommand, dbTransaction); 848 } 849 catch (SqlException e) 850 { 851 throw GetApplicationException(e); 852 } 853 catch (Exception e) 854 { 855 throw e; 856 } 857 finally 858 { 859 //CloseDatabase(sqlDatabase); 860 } 861 } 862 863 public static DataSet ExecuteTransDatasetProc(String strProcName, Dictionary<String, Object> dictParameter, ref Dictionary<String, Object> dictOutParameter, DbTransaction dbTransaction) 864 { 865 SqlDatabase sqlDatabase = GetDatabase(); 866 try 867 { 868 DataSet dataset; 869 DbCommand dbCommand = SetDbCommand(sqlDatabase.GetStoredProcCommand(strProcName)); 870 AddParaToCommand(dbCommand, dictParameter); 871 AddParaToCommandOutput(dbCommand, dictOutParameter); 872 dataset = sqlDatabase.ExecuteDataSet(dbCommand, dbTransaction); 873 AddCommandToPara(ref dictOutParameter, dbCommand); 874 return dataset; 875 } 876 catch (SqlException e) 877 { 878 throw GetApplicationException(e); 879 } 880 catch (Exception e) 881 { 882 throw e; 883 } 884 finally 885 { 886 //CloseDatabase(sqlDatabase); 887 } 888 } 889 #endregion 890 891 //在单独的事务中执行无返回Dataset的存储过程 892 #region "ExecuteNoQueryProc" 893 public static int ExecuteNoQueryProc(String strProcName, DbTransaction dbTransaction) 894 { 895 SqlDatabase sqlDatabase = GetDatabase(); 896 try 897 { 898 DbCommand dbCommand = SetDbCommand(sqlDatabase.GetStoredProcCommand(strProcName)); 899 return sqlDatabase.ExecuteNonQuery(dbCommand, dbTransaction); 900 } 901 catch (SqlException e) 902 { 903 throw GetApplicationException(e); 904 } 905 catch (Exception e) 906 { 907 throw e; 908 } 909 finally 910 { 911 //CloseDatabase(sqlDatabase); 912 } 913 } 914 915 public static int ExecuteNoQueryProc(String strProcName, Dictionary<String, Object> dictParameter, DbTransaction dbTransaction) 916 { 917 SqlDatabase sqlDatabase = GetDatabase(); 918 try 919 { 920 DbCommand dbCommand = SetDbCommand(sqlDatabase.GetStoredProcCommand(strProcName)); 921 AddParaToCommand(dbCommand, dictParameter); 922 return sqlDatabase.ExecuteNonQuery(dbCommand, dbTransaction); 923 } 924 catch (SqlException e) 925 { 926 throw GetApplicationException(e); 927 } 928 catch (Exception e) 929 { 930 throw e; 931 } 932 finally 933 { 934 //CloseDatabase(sqlDatabase); 935 } 936 } 937 938 public static int ExecuteNoQueryProc(String strProcName, Dictionary<String, Object> dictParameter, ref Dictionary<String, Object> dictOutParameter, DbTransaction dbTransaction) 939 { 940 SqlDatabase sqlDatabase = GetDatabase(); 941 try 942 { 943 int intAffected; 944 DbCommand dbCommand = SetDbCommand(sqlDatabase.GetStoredProcCommand(strProcName)); 945 AddParaToCommand(dbCommand, dictParameter); 946 AddParaToCommandOutput(dbCommand, dictOutParameter); 947 intAffected = sqlDatabase.ExecuteNonQuery(dbCommand, dbTransaction); 948 AddCommandToPara(ref dictOutParameter, dbCommand); 949 return intAffected; 950 } 951 catch (SqlException e) 952 { 953 throw GetApplicationException(e); 954 } 955 catch (Exception e) 956 { 957 throw e; 958 } 959 finally 960 { 961 //CloseDatabase(sqlDatabase); 962 } 963 } 964 #endregion 965 966 #region 加密解密方法 967 /// <summary> 968 /// 加密字符串 969 /// </summary> 970 /// <param name="pToEncrypt">加密内容</param> 971 /// <param name="sKey">密钥</param> 972 /// <returns></returns> 973 public static string Encrypt(string pToEncrypt, string sKey) 974 { 975 DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 976 byte[] inputByteArray; 977 inputByteArray = Encoding.Default.GetBytes(pToEncrypt); 978 //建立加密对象的密钥和偏移量 979 //原文使用ASCIIEncoding.ASCII方法的GetBytes方法 980 //使得输入密码必须输入英文文本 981 des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); 982 des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); 983 //写二进制数组到加密流 984 //(把内存流中的内容全部写入) 985 System.IO.MemoryStream ms = new System.IO.MemoryStream(); 986 CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); 987 988 989 //写二进制数组到加密流 990 //(把内存流中的内容全部写入) 991 cs.Write(inputByteArray, 0, inputByteArray.Length); 992 cs.FlushFinalBlock(); 993 // 建立输出字符串 994 StringBuilder ret = new StringBuilder(); 995 foreach (byte b in ms.ToArray()) 996 { 997 ret.AppendFormat("{0:X2}", b); 998 } 999 return ret.ToString(); 1000 } 1001 1002 /// <summary> 1003 /// 解密字符串 1004 /// </summary> 1005 /// <param name="pToDecrypt">解密内容</param> 1006 /// <param name="sKey">密钥</param> 1007 /// <returns></returns> 1008 public static string Decrypt(string pToDecrypt, string sKey) 1009 { 1010 DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 1011 // 把字符串放入byte数组 1012 int len; 1013 len = ((pToDecrypt.Length / 2) - 1); 1014 byte[] inputByteArray = new byte[len + 1]; 1015 int i; 1016 for (int x = 0; x <= len; x++) 1017 { 1018 i = Convert.ToInt32(pToDecrypt.Substring((x * 2), 2), 16); 1019 inputByteArray[x] = (byte)i; 1020 } 1021 //建立加密对象的密钥和偏移量,此值重要,不能修改 1022 des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); 1023 des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); 1024 System.IO.MemoryStream ms = new System.IO.MemoryStream(); 1025 CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); 1026 cs.Write(inputByteArray, 0, inputByteArray.Length); 1027 cs.FlushFinalBlock(); 1028 string Decrypt = Encoding.Default.GetString(ms.ToArray()); 1029 return Decrypt; 1030 } 1031 #endregion 1032 1033 #region "ExecuteStrNoQueryProc" 1034 public static string ExecuteStrNoQueryProc(String strProcName, Dictionary<String, Object> dictParameter, String strReturnPara, int size) 1035 { 1036 SqlDatabase sqlDatabase = GetDatabase(); 1037 try 1038 { 1039 DbCommand dbCommand = SetDbCommand(sqlDatabase.GetStoredProcCommand(strProcName)); 1040 //DbCommand dbCommand = sqlDatabase.GetStoredProcCommand(strProcName); 1041 AddParaToCommand(dbCommand, dictParameter); 1042 dbCommand.Parameters.Add(new SqlParameter(strReturnPara, SqlDbType.NVarChar, size)); 1043 dbCommand.Parameters[strReturnPara].Direction = ParameterDirection.Output; 1044 1045 1046 sqlDatabase.ExecuteNonQuery(dbCommand); 1047 1048 return dbCommand.Parameters[@strReturnPara].Value.ToString(); 1049 } 1050 catch (SqlException e) 1051 { 1052 throw GetApplicationException(e); 1053 } 1054 catch (Exception e) 1055 { 1056 throw e; 1057 } 1058 finally 1059 { 1060 CloseDatabase(sqlDatabase); 1061 } 1062 } 1063 #endregion 1064 1065 #region "ExecuteDatasetProcOutStr" 1066 public static DataSet ExecuteDatasetProcOutStr(String strProcName, Dictionary<String, Object> dictParameter, string strOutPara, out int intOutValue) 1067 { 1068 SqlDatabase sqlDatabase = GetDatabase(); 1069 try 1070 { 1071 DbCommand dbCommand = SetDbCommand(sqlDatabase.GetStoredProcCommand(strProcName)); 1072 AddParaToCommand(dbCommand, dictParameter); 1073 dbCommand.Parameters.Add(new SqlParameter(strOutPara, SqlDbType.Int)); 1074 dbCommand.Parameters[strOutPara].Direction = ParameterDirection.Output; 1075 1076 DataSet ds = null; 1077 1078 if (DBTRANSACTION == null) 1079 { 1080 ds = sqlDatabase.ExecuteDataSet(dbCommand); 1081 } 1082 else 1083 { 1084 ds = sqlDatabase.ExecuteDataSet(dbCommand, DBTRANSACTION); 1085 } 1086 1087 intOutValue = (int)dbCommand.Parameters[strOutPara].Value; 1088 1089 return ds; 1090 } 1091 catch (SqlException e) 1092 { 1093 throw GetApplicationException(e); 1094 } 1095 catch (Exception e) 1096 { 1097 throw e; 1098 } 1099 finally 1100 { 1101 CloseDatabase(sqlDatabase); 1102 } 1103 } 1104 public static DataSet ExecuteDatasetProcOutStr(String strProcName, Dictionary<String, Object> dictParameter, string strOutPara, ref string intOutValue) 1105 { 1106 SqlDatabase sqlDatabase = GetDatabase(); 1107 try 1108 { 1109 DbCommand dbCommand = SetDbCommand(sqlDatabase.GetStoredProcCommand(strProcName)); 1110 AddParaToCommand(dbCommand, dictParameter); 1111 dbCommand.Parameters.Add(new SqlParameter(strOutPara, SqlDbType.NVarChar)); 1112 dbCommand.Parameters[strOutPara].Direction = ParameterDirection.Output; 1113 1114 DataSet ds = null; 1115 1116 if (DBTRANSACTION == null) 1117 { 1118 ds = sqlDatabase.ExecuteDataSet(dbCommand); 1119 } 1120 else 1121 { 1122 ds = sqlDatabase.ExecuteDataSet(dbCommand, DBTRANSACTION); 1123 } 1124 1125 intOutValue = dbCommand.Parameters[strOutPara].Value.ToString(); 1126 1127 return ds; 1128 } 1129 catch (SqlException e) 1130 { 1131 throw GetApplicationException(e); 1132 } 1133 catch (Exception e) 1134 { 1135 throw e; 1136 } 1137 finally 1138 { 1139 CloseDatabase(sqlDatabase); 1140 } 1141 } 1142 public static DataSet ExecuteDatasetProcOutStr(String strProcName, Dictionary<String, Object> dictParameter, ref Dictionary<string,object> strOutParaValue) 1143 { 1144 SqlDatabase sqlDatabase = GetDatabase(); 1145 try 1146 { 1147 DbCommand dbCommand = SetDbCommand(sqlDatabase.GetStoredProcCommand(strProcName)); 1148 AddParaToCommand(dbCommand, dictParameter); 1149 foreach (string outPara in strOutParaValue.Keys) 1150 { 1151 dbCommand.Parameters.Add(new SqlParameter(outPara, SqlDbType.NVarChar, int.MaxValue)); 1152 dbCommand.Parameters[outPara].Direction = ParameterDirection.Output; 1153 } 1154 1155 DataSet ds = null; 1156 1157 if (DBTRANSACTION == null) 1158 { 1159 ds = sqlDatabase.ExecuteDataSet(dbCommand); 1160 } 1161 else 1162 { 1163 ds = sqlDatabase.ExecuteDataSet(dbCommand, DBTRANSACTION); 1164 } 1165 Dictionary<string, object> dictParaTemp = new Dictionary<string, object>(); 1166 foreach (KeyValuePair<string, object> kv in strOutParaValue) 1167 { 1168 dictParaTemp.Add(kv.Key.ToString(), kv.Value); 1169 } 1170 1171 foreach (KeyValuePair<string, object> kv in dictParaTemp) 1172 { 1173 strOutParaValue[kv.Key] = dbCommand.Parameters[kv.Key.ToString()].Value; 1174 } 1175 1176 return ds; 1177 } 1178 catch (SqlException e) 1179 { 1180 throw GetApplicationException(e); 1181 } 1182 catch (Exception e) 1183 { 1184 throw e; 1185 } 1186 finally 1187 { 1188 CloseDatabase(sqlDatabase); 1189 } 1190 } 1191 #endregion 1192 1193 /// <summary> 1194 /// 写日志 1195 /// </summary> 1196 /// <param name="ex"></param> 1197 public static void WriteLogInfo(string msgText) 1198 { 1199 try 1200 { 1201 string strLogFile = Application.StartupPath + "/log/AppLog/" + DateTime.Now.ToString("yyyyMMdd") + ".log"; 1202 1203 // 判断路径是否存在 1204 if (!Directory.Exists(Application.StartupPath + "/log/AppLog/")) 1205 { 1206 // 如果不存在则创建文件 1207 Directory.CreateDirectory(Application.StartupPath + "/log/AppLog/"); 1208 } 1209 1210 StreamWriter log = new StreamWriter(strLogFile, true); 1211 1212 log.WriteLine(DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + " " + msgText); 1213 1214 log.Close(); 1215 1216 } 1217 catch 1218 { 1219 1220 } 1221 } 1222 } 1223 }
标签:
原文地址:http://www.cnblogs.com/xuguanghui/p/4260217.html