SQLiteTransaction.cs SQLite事务

7 篇文章 0 订阅
4 篇文章 0 订阅


001
下载 SQLiteTransaction.cs

/*********************************************************
002  * ADO.NET 2.0 Data Provider for SQLite Version 3.X
003  * Written by Robert Simpson (robert@blackcastlesoft.com)
004  *
005  * Released to the public domain, use at your own risk!
006  ********************************************************/
007  
008 namespace Mono.Data.Sqlite
009 {
010   using System;
011   using System.Data;
012   using System.Data.Common;
013  
014   /// <summary>
015   /// SQLite implementation of DbTransaction.
016   /// </summary>
017   public sealed class SqliteTransaction : DbTransaction
018   {
019     /// <summary>
020     /// The connection to which this transaction is bound
021     /// </summary>
022     internal SqliteConnection _cnn;
023     internal long _version; // Matches the version of the connection
024     private IsolationLevel _level;
025  
026     /// <summary>
027     /// Constructs the transaction object, binding it to the supplied connection
028     /// </summary>
029     /// <param name="connection">The connection to open a transaction on</param>
030     /// <param name="deferredLock">TRUE to defer the writelock, or FALSE to lock immediately</param>
031     internal SqliteTransaction(SqliteConnection connection, bool deferredLock)
032     {
033       _cnn = connection;
034       _version = _cnn._version;
035  
036       _level = (deferredLock == true) ? IsolationLevel.ReadCommitted : IsolationLevel.Serializable;
037  
038       if (_cnn._transactionLevel++ == 0)
039       {
040         try
041         {
042           using (SqliteCommand cmd = _cnn.CreateCommand())
043           {
044             if (!deferredLock)
045               cmd.CommandText = "BEGIN IMMEDIATE";
046             else
047               cmd.CommandText = "BEGIN";
048  
049             cmd.ExecuteNonQuery();
050           }
051         }
052         catch (SqliteException)
053         {
054           _cnn._transactionLevel--;
055           _cnn = null;
056           throw;
057         }
058       }
059     }
060  
061     /// <summary>
062     /// Commits the current transaction.
063     /// </summary>
064     public override void Commit()
065     {
066       IsValid(true);
067  
068       if (_cnn._transactionLevel - 1 == 0)
069       {
070         using (SqliteCommand cmd = _cnn.CreateCommand())
071         {
072           cmd.CommandText = "COMMIT";
073           cmd.ExecuteNonQuery();
074         }
075       }
076       _cnn._transactionLevel--;
077       _cnn = null;
078     }
079  
080     /// <summary>
081     /// Returns the underlying connection to which this transaction applies.
082     /// </summary>
083     public new SqliteConnection Connection
084     {
085       get return _cnn; }
086     }
087  
088     /// <summary>
089     /// Forwards to the local Connection property
090     /// </summary>
091     protected override DbConnection DbConnection
092     {
093       get return Connection; }
094     }
095  
096     /// <summary>
097     /// Disposes the transaction.  If it is currently active, any changes are rolled back.
098     /// </summary>
099     protected override void Dispose(bool disposing)
100     {
101       if (disposing)
102       {
103         lock (this)
104         {
105           if (IsValid(false))
106             Rollback();
107  
108           _cnn = null;
109         }
110       }
111       base.Dispose(disposing);
112     }
113  
114     /// <summary>
115     /// Gets the isolation level of the transaction.  SQLite only supports Serializable transactions.
116     /// </summary>
117     public override IsolationLevel IsolationLevel
118     {
119       get return _level; }
120     }
121  
122     /// <summary>
123     /// Rolls back the active transaction.
124     /// </summary>
125     public override void Rollback()
126     {
127       IsValid(true);
128  
129       IssueRollback(_cnn);
130  
131       _cnn._transactionLevel = 0;
132       _cnn = null;
133     }
134  
135     internal static void IssueRollback(SqliteConnection cnn)
136     {
137       using (SqliteCommand cmd = cnn.CreateCommand())
138       {
139         cmd.CommandText = "ROLLBACK";
140         cmd.ExecuteNonQuery();
141       }
142     }
143  
144     internal bool IsValid(bool throwError)
145     {
146       if (_cnn == null)
147       {
148         if (throwError == truethrow new ArgumentNullException("No connection associated with this transaction");
149         else return false;
150       }
151  
152       if (_cnn._transactionLevel == 0)
153       {
154         if (throwError == truethrow new SqliteException((int)SQLiteErrorCode.Misuse, "No transaction is active on this connection");
155         else return false;
156       }
157       if (_cnn._version != _version)
158       {
159         if (throwError == truethrow new SqliteException((int)SQLiteErrorCode.Misuse, "The connection was closed and re-opened, changes were rolled back");
160         else return false;
161       }
162       if (_cnn.State != ConnectionState.Open)
163       {
164         if (throwError == truethrow new SqliteException((int)SQLiteErrorCode.Misuse,"Connection was closed");
165         else return false;
166       }
167  
168       return true;
169     }
170   }
171 }
001 /*********************************************************
002  * ADO.NET 2.0 Data Provider for SQLite Version 3.X
003  * Written by Robert Simpson (robert@blackcastlesoft.com)
004  *
005  * Released to the public domain, use at your own risk!
006  ********************************************************/
007  
008 namespace Mono.Data.Sqlite
009 {
010   using System;
011   using System.Data;
012   using System.Data.Common;
013  
014   /// <summary>
015   /// SQLite implementation of DbTransaction.
016   /// </summary>
017   public sealed class SqliteTransaction : DbTransaction
018   {
019     /// <summary>
020     /// The connection to which this transaction is bound
021     /// </summary>
022     internal SqliteConnection _cnn;
023     internal long _version; // Matches the version of the connection
024     private IsolationLevel _level;
025  
026     /// <summary>
027     /// Constructs the transaction object, binding it to the supplied connection
028     /// </summary>
029     /// <param name="connection">The connection to open a transaction on</param>
030     /// <param name="deferredLock">TRUE to defer the writelock, or FALSE to lock immediately</param>
031     internal SqliteTransaction(SqliteConnection connection, bool deferredLock)
032     {
033       _cnn = connection;
034       _version = _cnn._version;
035  
036       _level = (deferredLock == true) ? IsolationLevel.ReadCommitted : IsolationLevel.Serializable;
037  
038       if (_cnn._transactionLevel++ == 0)
039       {
040         try
041         {
042           using (SqliteCommand cmd = _cnn.CreateCommand())
043           {
044             if (!deferredLock)
045               cmd.CommandText = "BEGIN IMMEDIATE";
046             else
047               cmd.CommandText = "BEGIN";
048  
049             cmd.ExecuteNonQuery();
050           }
051         }
052         catch (SqliteException)
053         {
054           _cnn._transactionLevel--;
055           _cnn = null;
056           throw;
057         }
058       }
059     }
060  
061     /// <summary>
062     /// Commits the current transaction.
063     /// </summary>
064     public override void Commit()
065     {
066       IsValid(true);
067  
068       if (_cnn._transactionLevel - 1 == 0)
069       {
070         using (SqliteCommand cmd = _cnn.CreateCommand())
071         {
072           cmd.CommandText = "COMMIT";
073           cmd.ExecuteNonQuery();
074         }
075       }
076       _cnn._transactionLevel--;
077       _cnn = null;
078     }
079  
080     /// <summary>
081     /// Returns the underlying connection to which this transaction applies.
082     /// </summary>
083     public new SqliteConnection Connection
084     {
085       get return _cnn; }
086     }
087  
088     /// <summary>
089     /// Forwards to the local Connection property
090     /// </summary>
091     protected override DbConnection DbConnection
092     {
093       get return Connection; }
094     }
095  
096     /// <summary>
097     /// Disposes the transaction.  If it is currently active, any changes are rolled back.
098     /// </summary>
099     protected override void Dispose(bool disposing)
100     {
101       if (disposing)
102       {
103         lock (this)
104         {
105           if (IsValid(false))
106             Rollback();
107  
108           _cnn = null;
109         }
110       }
111       base.Dispose(disposing);
112     }
113  
114     /// <summary>
115     /// Gets the isolation level of the transaction.  SQLite only supports Serializable transactions.
116     /// </summary>
117     public override IsolationLevel IsolationLevel
118     {
119       get return _level; }
120     }
121  
122     /// <summary>
123     /// Rolls back the active transaction.
124     /// </summary>
125     public override void Rollback()
126     {
127       IsValid(true);
128  
129       IssueRollback(_cnn);
130  
131       _cnn._transactionLevel = 0;
132       _cnn = null;
133     }
134  
135     internal static void IssueRollback(SqliteConnection cnn)
136     {
137       using (SqliteCommand cmd = cnn.CreateCommand())
138       {
139         cmd.CommandText = "ROLLBACK";
140         cmd.ExecuteNonQuery();
141       }
142     }
143  
144     internal bool IsValid(bool throwError)
145     {
146       if (_cnn == null)
147       {
148         if (throwError == truethrow new ArgumentNullException("No connection associated with this transaction");
149         else return false;
150       }
151  
152       if (_cnn._transactionLevel == 0)
153       {
154         if (throwError == truethrow new SqliteException((int)SQLiteErrorCode.Misuse, "No transaction is active on this connection");
155         else return false;
156       }
157       if (_cnn._version != _version)
158       {
159         if (throwError == truethrow new SqliteException((int)SQLiteErrorCode.Misuse, "The connection was closed and re-opened, changes were rolled back");
160         else return false;
161       }
162       if (_cnn.State != ConnectionState.Open)
163       {
164         if (throwError == truethrow new SqliteException((int)SQLiteErrorCode.Misuse,"Connection was closed");
165         else return false;
166       }
167  
168       return true;
169     }
170   }
171 }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLiteHelper.cs是一个用于操作SQLite数据库的辅助类。SQLite是一种嵌入式关系型数据库,它以文件形式存储数据,非常轻量级且易于使用。 在SQLiteHelper.cs中,通常会包含以下几个主要功能: 1. 连接数据库:SQLiteHelper.cs提供了连接数据库的方法,通过传入数据库文件路径等参数,可以建立与SQLite数据库的连接。 2. 创建表格:通过SQLiteHelper.cs,可以执行SQL语句来创建数据库表格。可以根据实际需求定义表的结构、字段及其属性,并创建相应的索引和约束。 3. 插入数据:SQLiteHelper.cs通过执行SQL语句来向数据库表中插入数据。可以将数据对象转换为SQL语句执行插入操作,实现数据的持久化存储。 4. 更新数据:可以通过SQLiteHelper.cs执行SQL语句来更新数据库表中的数据。可以根据具体需求使用UPDATE语句来修改数据。 5. 删除数据:通过SQLiteHelper.cs执行SQL语句来删除数据库表中的数据。根据具体需求可以使用DELETE语句来删除指定的数据行。 6. 查询数据:通过SQLiteHelper.cs执行SQL语句来查询数据库表中的数据。可以根据条件进行查询,并获取结果集,用于后续的数据处理。 7. 数据库事务处理:SQLiteHelper.cs提供了支持数据库事务的方法。可以通过开启事务、提交或回滚事务来确保数据的完整性和一致性。 SQLiteHelper.cs提供了简单而便捷的接口,使得开发者能够快速地使用SQLite数据库进行数据的存储和操作。无论是移动应用、桌面应用还是嵌入式设备,都可以使用SQLiteHelper.cs来方便地管理数据库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值