TransactionGroup.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace LJLib.SQLEX
  10. {
  11. public class TransactionGroup : IDisposable
  12. {
  13. private List<LJTransactionScope> _transactions;
  14. public TransactionGroup(IEnumerable<SqlCommand> cmds)
  15. {
  16. _transactions = new List<LJTransactionScope>();
  17. foreach (var cmd in cmds)
  18. {
  19. if (cmd == null)
  20. {
  21. continue;
  22. }
  23. if (cmd.Connection == null)
  24. {
  25. continue;
  26. }
  27. if (cmd.Connection.State != ConnectionState.Open)
  28. {
  29. continue;
  30. }
  31. _transactions.Add(new LJTransactionScope(cmd));
  32. }
  33. }
  34. public void Commit()
  35. {
  36. foreach (var ts in _transactions)
  37. {
  38. try
  39. {
  40. ts.Complete();
  41. }
  42. catch (Exception e)
  43. {
  44. Trace.Write(e);
  45. }
  46. }
  47. }
  48. public void Dispose()
  49. {
  50. foreach (var ts in _transactions)
  51. {
  52. try
  53. {
  54. ts.Dispose();
  55. }
  56. catch (Exception e)
  57. {
  58. Trace.Write(e);
  59. }
  60. }
  61. }
  62. }
  63. }