2
0

SqlTransaction.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // System.Data.SqlClient.SqlTransaction.cs
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. //
  7. // Copyright (C) Tim Coleman, 2002
  8. //
  9. using System;
  10. using System.Data;
  11. using System.Data.Common;
  12. namespace System.Data.SqlClient {
  13. public sealed class SqlTransaction : MarshalByRefObject, IDbTransaction, IDisposable
  14. {
  15. #region Fields
  16. bool disposed = false;
  17. SqlConnection connection;
  18. IsolationLevel isolationLevel;
  19. bool isOpen;
  20. #endregion
  21. #region Constructors
  22. internal SqlTransaction (SqlConnection connection, IsolationLevel isolevel)
  23. {
  24. this.connection = connection;
  25. this.isolationLevel = isolevel;
  26. isOpen = true;
  27. }
  28. #endregion // Constructors
  29. #region Properties
  30. public SqlConnection Connection {
  31. get { return connection; }
  32. }
  33. internal bool IsOpen {
  34. get { return isOpen; }
  35. }
  36. public IsolationLevel IsolationLevel {
  37. get { return isolationLevel; }
  38. }
  39. IDbConnection IDbTransaction.Connection {
  40. get { return Connection; }
  41. }
  42. #endregion // Properties
  43. #region Methods
  44. public void Commit ()
  45. {
  46. if (!isOpen)
  47. throw new InvalidOperationException ("The Transaction was not open.");
  48. connection.Tds.Execute ("COMMIT TRANSACTION");
  49. connection.Transaction = null;
  50. isOpen = false;
  51. }
  52. private void Dispose (bool disposing)
  53. {
  54. if (!disposed) {
  55. if (disposing) {
  56. Rollback ();
  57. }
  58. disposed = true;
  59. }
  60. }
  61. public void Dispose ()
  62. {
  63. Dispose (true);
  64. GC.SuppressFinalize (this);
  65. }
  66. public void Rollback ()
  67. {
  68. Rollback (String.Empty);
  69. }
  70. public void Rollback (string transactionName)
  71. {
  72. if (!isOpen)
  73. throw new InvalidOperationException ("The Transaction was not open.");
  74. connection.Tds.Execute (String.Format ("ROLLBACK TRANSACTION {0}", transactionName));
  75. isOpen = false;
  76. }
  77. public void Save (string savePointName)
  78. {
  79. if (!isOpen)
  80. throw new InvalidOperationException ("The Transaction was not open.");
  81. connection.Tds.Execute (String.Format ("SAVE TRANSACTION {0}", savePointName));
  82. }
  83. #endregion // Methods
  84. }
  85. }