dotConnect for DB2 Documentation
Devart.Data.DB2 Namespace / DB2Transaction Class
Members Example

DB2Transaction Class
Represents a SQL transaction to be made in the DB2 database.
Syntax
'Declaration
 
Public Class DB2Transaction 
   Inherits Devart.Common.DbTransactionBase
   Implements System.Data.IDbTransactionSystem.IDisposable 
 
Remarks

The application creates a DB2Transaction object by calling DB2Connection.BeginTransaction on the DB2Connection object. All subsequent operations associated with the transaction (for example, committing or aborting the transaction), are performed on the DB2Transaction object.

The correlation between DB2Connection and DB2Transaction is always 1:1.

Example
The following example creates a DB2Connection and a DB2Transaction. It also demonstrates how to use the DB2Connection.BeginTransaction, Commit, and Rollback methods.
public static void RunDB2Transaction(string myConnString) {
        using (DB2Connection db2Connection = new DB2Connection(myConnString)) {
                db2Connection.Open();
                // Start a local transaction
                DB2Transaction myTrans = db2Connection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
                DB2Command db2Command = db2Connection.CreateCommand();
                try {
                        db2Command.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(52, 'DEVELOPMENT')";
                        db2Command.ExecuteNonQuery();
                        db2Command.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(62, 'PRODUCTION')";
                        db2Command.ExecuteNonQuery();
                        myTrans.Commit();
                        Console.WriteLine("Both records are written to database.");
                }
                catch (Exception e) {
                        myTrans.Rollback();
                        Console.WriteLine(e.ToString());
                        Console.WriteLine("Neither record was written to database.");
                }
                finally {
                        db2Command.Dispose();
                        myTrans.Dispose();
                }
    }
}
Public Shared Sub RunDB2Transaction(myConnString As String)
        Using db2Connection As New DB2Connection(myConnString)
                db2Connection.Open()
                ' Start a local transaction
                Dim myTrans As DB2Transaction = db2Connection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted)
                Dim db2Command As DB2Command = db2Connection.CreateCommand()
                Try
                        db2Command.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(52, 'DEVELOPMENT')"
                        db2Command.ExecuteNonQuery()
                        db2Command.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(62, 'PRODUCTION')"
                        db2Command.ExecuteNonQuery()
                        myTrans.Commit()
                        Console.WriteLine("Both records are written to database.")
                Catch e As Exception
                        myTrans.Rollback()
                        Console.WriteLine(e.ToString())
                        Console.WriteLine("Neither record was written to database.")
                Finally
                        db2Command.Dispose()
                        myTrans.Dispose()
                End Try
    End Using
End Sub
Inheritance Hierarchy

System.Object
   System.MarshalByRefObject
      System.Data.Common.DbTransaction
         Devart.Common.DbTransactionBase
            Devart.Data.DB2.DB2Transaction

Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also