SqlTransaction.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. SqlConnection connection;
  18. IsolationLevel isolationLevel;
  19. bool isOpen;
  20. #endregion
  21. #region Constructors
  22. internal SqlTransaction (SqlConnection connection, IsolationLevel isolevel)
  23. {
  24. SetIsolationLevel (connection, isolevel);
  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. static void SetIsolationLevel (SqlConnection connection, IsolationLevel isolevel)
  46. {
  47. string commandText = "SET TRANSACTION ISOLATION LEVEL ";
  48. switch (isolevel) {
  49. case IsolationLevel.Chaos :
  50. commandText += "CHAOS";
  51. break;
  52. case IsolationLevel.ReadCommitted :
  53. commandText += "READ COMMITTED";
  54. break;
  55. case IsolationLevel.ReadUncommitted :
  56. commandText += "READ UNCOMMITTED";
  57. break;
  58. case IsolationLevel.RepeatableRead :
  59. commandText += "REPEATABLE READ";
  60. break;
  61. case IsolationLevel.Serializable :
  62. commandText += "SERIALIZABLE";
  63. break;
  64. default :
  65. return;
  66. }
  67. connection.Tds.ExecuteNonQuery (commandText);
  68. connection.CheckForErrors ();
  69. }
  70. public void Commit ()
  71. {
  72. if (!isOpen)
  73. throw new InvalidOperationException ("The Transaction was not open.");
  74. connection.Tds.ExecuteNonQuery ("IF @@TRANCOUNT>0 COMMIT TRAN");
  75. isOpen = false;
  76. }
  77. [MonoTODO]
  78. public void Dispose ()
  79. {
  80. throw new NotImplementedException ();
  81. }
  82. public void Rollback ()
  83. {
  84. if (!isOpen)
  85. throw new InvalidOperationException ("The Transaction was not open.");
  86. connection.Tds.ExecuteNonQuery ("IF @@TRANCOUNT>0 ROLLBACK TRAN");
  87. connection.CheckForErrors ();
  88. isOpen = false;
  89. }
  90. public void Rollback (string transactionName)
  91. {
  92. if (!isOpen)
  93. throw new InvalidOperationException ("The Transaction was not open.");
  94. connection.Tds.ExecuteNonQuery (String.Format ("IF @@TRANCOUNT > 0 ROLLBACK TRAN {0}", transactionName));
  95. connection.CheckForErrors ();
  96. isOpen = false;
  97. }
  98. public void Save (string savePointName)
  99. {
  100. if (!isOpen)
  101. throw new InvalidOperationException ("The Transaction was not open.");
  102. connection.Tds.ExecuteNonQuery (String.Format ("SAVE TRAN {0}", savePointName));
  103. connection.CheckForErrors ();
  104. }
  105. #endregion // Methods
  106. }
  107. }