代表一个GBase 数据库中的事务。这个类不能被继承。对于该类型的所有 成员,参考GBaseTransaction 成员。 继承层次 System.Object |__ System.MarshalByRefObject |__ System.Data.Common.DbTransaction |__ GBase.Data.GBaseClient.GBaseTransaction 语法 [Visual Basic] Public NotInheritable Class GBaseTransaction _ Inherits DbTransaction [C#] public sealed class GBaseTransaction : DbTransaction 必要条件
GBase 8a 程序员手册ADO.NET 篇 南大通用数据技术股份有限公司
- 323 - 命名空间:GBase.Data.GBaseClient 线程安全性 这个类型的公共静态成员(在Visual Basic 中为 Shared)对于多线程操作 是保证线程安全的,对于实例不保证线程安全性。 注释 程序通过在GBaseConnection 对象上调用BeginTransaction 来创建一个 GBaseTransaction 对象。 所有后面关于事务的操作 (例如: 提交或者回滚事务) 都在GBaseTransaction 对象上进行。 示例 下面的例子创建了一个GBaseConnection 和一个GBaseTransaction 对象, 还示范了如何使用BeginTransaction、Commit 和Rollback 方法。 [Visual Basic] Public Sub RunTransaction(gsConnString As String) Dim gsConnection As New GBaseConnection(gsConnString) gsConnection.Open() Dim gsCommand As GBaseCommand = gsConnection.CreateCommand() Dim gsTrans As GBaseTransaction ' Start a local transaction gsTrans = gsConnection.BeginTransaction() ' Must assign both transaction object and connection ' to Command object for a pending local transaction gsCommand.Connection = gsConnection gsCommand.Transaction = gsTrans Try gsCommand.CommandText = "Insert into Region ("_ &"RegionID,"_ &"RegionDescription)VALUES (100, "_ &"Description')" gsCommand.ExecuteNonQuery() gsCommand.CommandText = "Insert into Region ("_ &"RegionID, "_
GBase 8a 程序员手册ADO.NET 篇
- 324 -
南大通用数据技术股份有限公司 &"RegionDescription) VALUES (101, "_ &"'Description')" gsCommand.ExecuteNonQuery() gsTrans.Commit() Console.WriteLine("Both records are written to database.") Catch e As Exception Try gsTrans.Rollback() Catch ex As GBaseException If Not gsTrans.Connection Is Nothing Then Console.WriteLine("An exception of type " &_ ex.GetType().ToString() &_ " was encountered while attempting to roll back the transaction.") End If End Try Console.WriteLine("An exception of type "& _ e.GetType().ToString()&_ "was encountered while inserting the data.") Console.WriteLine("Neither record was written to database.") Finally gsConnection.Close() End Try End Sub 'RunTransaction
[C#] public void RunTransaction(string gsConnString) { GBaseConnection gsConnection = new GBaseConnection(gsConnString); gsConnection.Open(); GBaseCommand gsCommand = gsConnection.CreateCommand(); GBaseTransaction gsTrans; // Start a local transaction gsTrans = gsConnection.BeginTransaction();
GBase 8a 程序员手册ADO.NET 篇 南大通用数据技术股份有限公司
- 325 - // Must assign both transaction object and connection // to Command object for a pending local transaction gsCommand.Connection = gsConnection; gsCommand.Transaction = gsTrans; try { gsCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')"; gsCommand.ExecuteNonQuery();
gsCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')"; gsCommand.ExecuteNonQuery(); gsTrans.Commit(); Console.WriteLine("Both records are written to database."); } catch(Exception e) { try { gsTrans.Rollback(); } catch (GBaseException ex) { if (gsTrans.Connection != null) { Console.WriteLine("An exception of type " + ex.GetType() +" was encountered while attempting to roll back the transaction."); } } Console.WriteLine("An exception of type " + e.GetType() + " was encountered while inserting the data."); Console.WriteLine("Neither record was written to database."); } finally