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

In This Topic
    Commit Method
    In This Topic
    Commits the database transaction.
    Syntax
    'Declaration
     
    Public Overrides Sub Commit() 
    public override void Commit()
    Remarks
    The Commit method is equivalent to the SQL COMMIT statement.
    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