SQLiteTransaction.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Data;
  3. using System.Collections;
  4. using System.Text;
  5. namespace System.Data.SQLiteClient
  6. {
  7. sealed public class SQLiteTransaction : IDbTransaction, IDisposable
  8. {
  9. private SQLiteConnection _Connection;
  10. internal SQLiteTransaction(SQLiteConnection connection)
  11. {
  12. _Connection = connection;
  13. if (_Connection == null) throw new ArgumentNullException("Connection cannot be null.");
  14. Execute("BEGIN");
  15. }
  16. public void Dispose()
  17. {
  18. if (_Connection != null && _Connection.State != ConnectionState.Closed)
  19. {
  20. Rollback();
  21. }
  22. }
  23. IDbConnection IDbTransaction.Connection
  24. {
  25. get { return Connection; }
  26. }
  27. public SQLiteConnection Connection
  28. {
  29. get { return _Connection; }
  30. }
  31. public IsolationLevel IsolationLevel
  32. {
  33. get { return IsolationLevel.Unspecified; }
  34. }
  35. public void Commit()
  36. {
  37. Execute("COMMIT");
  38. }
  39. public void Rollback()
  40. {
  41. Execute("ROLLBACK");
  42. }
  43. private void Execute(string command)
  44. {
  45. if (_Connection == null || _Connection.State != ConnectionState.Open) throw new SQLiteException("Connection is not open.");
  46. SQLiteCommand cmd = _Connection.CreateCommand(command);
  47. cmd.ExecuteNonQuery();
  48. }
  49. }
  50. }