dotConnect for DB2 Documentation
Devart.Data.DB2 Namespace / DB2Connection Class / BeginTransaction Method / BeginTransaction(IsolationLevel) Method
Example

In This Topic
    BeginTransaction(IsolationLevel) Method
    In This Topic
    Begins a database transaction with the specified isolation level.
    Syntax
    'Declaration
     
    Public Overloads Shadows Function BeginTransaction( _
       ByVal il As IsolationLevel _
    ) As DB2Transaction
    public new DB2Transaction BeginTransaction( 
       IsolationLevel il
    )

    Parameters

    il

    Return Value

    An object representing the new transaction.
    Remarks
    To commit or roll back the transaction, you must explicitly use the Devart.Data.DB2.DB2Connection.Commit() or Devart.Data.DB2.DB2Connection.Rollback() methods.
    Example
    The following example creates a DB2Connection and a DB2Transaction. It also demonstrates how to use the BeginTransaction(), Devart.Data.DB2.DB2Connection.Commit, and Devart.Data.DB2.DB2Connection.Rollback methods.
    public void RunDB2Transaction(string myConnString)
    {
      DB2Connection db2Connection = new DB2Connection(myConnString);
      db2Connection.Open();
    
      DB2Command db2Command = new DB2Command();
      db2Command.Connection = db2Connection;
      DB2Transaction myTrans;
    
      // Start a local transaction
      myTrans = db2Connection.BeginTransaction(IsolationLevel.ReadCommitted);
      // Assign transaction object for a pending local transaction
      db2Command.Transaction = myTrans;
    
      try
      {
        db2Command.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(50, 'DEVELOPMENT')";
        db2Command.ExecuteNonQuery();
        db2Command.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(60, '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
      {
        db2Connection.Close();
      }
    }
    Public Sub RunDB2Transaction(myConnString As String)
      Dim db2Connection As New DB2Connection(myConnString)
      db2Connection.Open()
    
      Dim db2Command As New DB2Command()
      db2Command.Connection = db2Connection
      Dim myTrans As DB2Transaction
    
      ' Start a local transaction
      myTrans = db2Connection.BeginTransaction(IsolationLevel.ReadCommitted)
      ' Assign transaction object for a pending local transaction
      db2Command.Transaction = myTrans
    
      Try
        db2Command.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(50, 'DEVELOPMENT')"
        db2Command.ExecuteNonQuery()
        db2Command.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(60, '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
        db2Connection.Close()
      End Try
    End Sub
    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

    Reference

    DB2Connection Class
    DB2Connection Members
    Overload List
    DB2Command Class
    DB2Transaction Class
    Devart.Data.DB2.DB2Connection.Commit
    Devart.Data.DB2.DB2Connection.Rollback