SqlTransaction.cs 2.1 KB

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