dotConnect for DB2 Documentation
Devart.Data.DB2 Namespace / DB2Transaction Class / Rollback Method / Rollback() Method
Example

In This Topic
    Rollback() Method
    In This Topic
    Rolls back a database transaction from a pending state.
    Syntax
    'Declaration
     
    Public Overloads Overrides Sub Rollback() 
    public override void Rollback()
    Remarks
    The Rollback method is equivalent to the SQL ROLLBACK statement. The transaction can only be rolled back from a pending state (after BeginTransaction has been called, but before Commit is called).
    Example
    The following example creates a DB2Connection and a DB2Transaction. It also demonstrates how to use the BeginTransaction, Commit, and Rollback methods.
    public void RunDB2Transaction(string myConnString)
    {
      DB2Connection db2Connection = new DB2Connection(myConnString);
      db2Connection.Open();
    
      DB2Command db2Command = new DB2Command();
      DB2Transaction myTrans;
    
      // Start a local transaction
      myTrans = db2Connection.BeginTransaction();
      // 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()
      Dim myTrans As DB2Transaction
    
      ' Start a local transaction
      myTrans = db2Connection.BeginTransaction()
      ' 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

    DB2Transaction Class
    DB2Transaction Members
    Overload List
    BeginTransaction Method
    Commit Method
    Devart.Data.DB2.DB2Transaction.Save(System.String)