dotConnect for MySQL Documentation
Devart.Data.MySql Namespace / MySqlConnection Class / Rollback Method
Example

In This Topic
    Rollback Method (MySqlConnection)
    In This Topic
    Rolls back a transaction from a pending state.
    Syntax
    'Declaration
     
    Public Sub Rollback() 
    public void Rollback()
    Remarks

    In MySQL, only InnoDB and BDB (BerkeleyDB) table formats support transactions. Other standard storage engines ignore transactions and can not rollback data modifications.

    Example
    The following example demonstrates how to use Commit and Rollback methods.
    public void RunMySqlTransaction(string myConnString)
    {
      MySqlConnection myConnection = new MySqlConnection(myConnString);
      myConnection.Open();
    
      MySqlCommand myCommand = new MySqlCommand();
      myConnection.BeginTransaction();
      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();
        myConnection.Commit();
        Console.WriteLine("Both records are written to database.");
      }
      catch(Exception e)
      {
        myConnection.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()
      myConnection.BeginTransaction()
      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()
        myConnection.Commit()
        Console.WriteLine("Both records are written to database.")
      Catch e As Exception
        myConnection.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