dotConnect for SQLite Documentation
Devart.Data.SQLite Namespace / SQLiteTransaction Class / Rollback Method
Example

In This Topic
    Rollback Method (SQLiteTransaction)
    In This Topic
    Rolls back a database transaction from a pending state.
    Syntax
    'Declaration
     
    Public 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 SQLiteConnection.BeginTransaction has been called, but before Commit is called).
    Example
    The following example creates a SQLiteConnection and a SQLiteTransaction. It also demonstrates how to use the SQLiteConnection.BeginTransaction, Commit, and Rollback methods.
    public void RunSQLiteTransaction(string myConnString)
    {
      SQLiteConnection sqConnection = new SQLiteConnection(myConnString);
      sqConnection.Open();
    
      SQLiteCommand sqCommand = new SQLiteCommand();
      SQLiteTransaction myTrans;
    
      // Start a local transaction
      myTrans = sqConnection.BeginTransaction();
      // Assign transaction object for a pending local transaction
      sqCommand.Transaction = myTrans;
    
      try
      {
        sqCommand.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(50, 'DEVELOPMENT')";
        sqCommand.ExecuteNonQuery();
        sqCommand.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(60, 'PRODUCTION')";
        sqCommand.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
      {
        sqConnection.Close();
      }
    }
    Public Sub RunSQLiteTransaction(myConnString As String)
      Dim sqConnection As New SQLiteConnection(myConnString)
      sqConnection.Open()
    
      Dim sqCommand As New SQLiteCommand()
      Dim myTrans As SQLiteTransaction
    
      ' Start a local transaction
      myTrans = sqConnection.BeginTransaction()
      ' Assign transaction object for a pending local transaction
      sqCommand.Transaction = myTrans
    
      Try
        sqCommand.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(50, 'DEVELOPMENT')"
        sqCommand.ExecuteNonQuery()
        sqCommand.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(60, 'PRODUCTION')"
        sqCommand.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
        sqConnection.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

    SQLiteTransaction Class
    SQLiteTransaction Members
    BeginTransaction Method
    Commit Method
    Devart.Data.SQLite.SQLiteTransaction.Save(System.String)