dotConnect for MySQL Documentation
Devart.Data.MySql Namespace / MySqlTransaction 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 MySqlConnection.BeginTransaction has been called, but before Commit is called).
    Example
    The following example creates a MySqlConnection and a MySqlTransaction. It also demonstrates how to use the MySqlConnection.BeginTransaction, Commit, and Rollback methods.
    public void RunMySqlTransaction(string myConnString)
    {
      MySqlConnection myConnection = new MySqlConnection(myConnString);
      myConnection.Open();
    
      MySqlCommand myCommand = new MySqlCommand();
      MySqlTransaction myTrans;
    
      // Start a local transaction
      myTrans = myConnection.BeginTransaction();
      // Assign transaction object for a pending local transaction
      myCommand.Transaction = myTrans;
    
      try
      {
        myCommand.CommandText = "INSERT INTO Test.Dept(DeptNo, DName) Values(50, 'DEVELOPMENT')";
        myCommand.ExecuteNonQuery();
        myCommand.CommandText = "INSERT INTO Test.Dept(DeptNo, DName) Values(60, 'PRODUCTION')";
        myCommand.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
      {
        myConnection.Close();
      }
    }
    Public Sub RunMySqlTransaction(myConnString As String)
      Dim myConnection As New MySqlConnection(myConnString)
      myConnection.Open()
    
      Dim myCommand As New MySqlCommand()
      Dim myTrans As MySqlTransaction
    
      ' Start a local transaction
      myTrans = myConnection.BeginTransaction()
      ' Assign transaction object for a pending local transaction
      myCommand.Transaction = myTrans
    
      Try
        myCommand.CommandText = "INSERT INTO Test.Dept(DeptNo, DName) Values(50, 'DEVELOPMENT')"
        myCommand.ExecuteNonQuery()
        myCommand.CommandText = "INSERT INTO Test.Dept(DeptNo, DName) Values(60, 'PRODUCTION')"
        myCommand.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
        myConnection.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