dotConnect for PostgreSQL Documentation
Devart.Data.PostgreSql Namespace / PgSqlConnection Class / Rollback Method
Example

In This Topic
    Rollback Method (PgSqlConnection)
    In This Topic
    Rolls back a transaction from a pending state.
    Syntax
    'Declaration
     
    Public Sub Rollback() 
    public void Rollback()
    Example
    The following example demonstrates how to use Commit and Rollback methods.
    public void RunPgSqlTransaction(string myConnString)
    {
      PgSqlConnection pgConnection = new PgSqlConnection(myConnString);
      pgConnection.Open();
    
      PgSqlCommand pgCommand = new PgSqlCommand();
      pgConnection.BeginTransaction();
      try
      {
        pgCommand.CommandText = "INSERT INTO Test.Dept(DeptNo, DName) Values(50, 'DEVELOPMENT')";
        pgCommand.ExecuteNonQuery();
        pgCommand.CommandText = "INSERT INTO Test.Dept(DeptNo, DName) Values(60, 'PRODUCTION')";
        pgCommand.ExecuteNonQuery();
        pgConnection.Commit();
        Console.WriteLine("Both records are written to database.");
      }
      catch(Exception e)
      {
        pgConnection.Rollback();
        Console.WriteLine(e.ToString());
        Console.WriteLine("Neither record was written to database.");
      }
      finally
      {
        pgConnection.Close();
      }
    }
    Public Sub RunPgSqlTransaction(myConnString As String)
      Dim pgConnection As New PgSqlConnection(myConnString)
      pgConnection.Open()
    
      Dim pgCommand As New PgSqlCommand()
      pgConnection.BeginTransaction()
      Try
        pgCommand.CommandText = "INSERT INTO Test.Dept(DeptNo, DName) Values(50, 'DEVELOPMENT')"
        pgCommand.ExecuteNonQuery()
        pgCommand.CommandText = "INSERT INTO Test.Dept(DeptNo, DName) Values(60, 'PRODUCTION')"
        pgCommand.ExecuteNonQuery()
        pgConnection.Commit()
        Console.WriteLine("Both records are written to database.")
      Catch e As Exception
        pgConnection.Rollback()
        Console.WriteLine(e.ToString())
        Console.WriteLine("Neither record was written to database.")
      Finally
        pgConnection.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