dotConnect for PostgreSQL Documentation
Devart.Data.PostgreSql Namespace / PgSqlTransaction Class
Members Example

In This Topic
    PgSqlTransaction Class
    In This Topic
    Represents a SQL transaction to be made in the PostgreSQL database.
    Syntax
    'Declaration
     
    Public Class PgSqlTransaction 
       Inherits Devart.Common.DbTransactionBase
       Implements System.Data.IDbTransactionSystem.IDisposable 
    Remarks

    The application creates a PgSqlTransaction object by calling PgSqlConnection.BeginTransaction on the PgSqlConnection object. All subsequent operations associated with the transaction (for example, committing or aborting the transaction), are performed on the PgSqlTransaction object.

    The correlation between PgSqlConnection and PgSqlTransaction is always 1:1.

    Example
    The following example creates a PgSqlConnection and a PgSqlTransaction. It also demonstrates how to use the PgSqlConnection.BeginTransaction, Commit, and Rollback methods.
    public static void RunPgSqlTransaction(string myConnString) {
            using (PgSqlConnection pgConnection = new PgSqlConnection(myConnString)) {
                    pgConnection.Open();
                    // Start a local transaction
                    PgSqlTransaction myTrans = pgConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
                    PgSqlCommand pgCommand = pgConnection.CreateCommand();
                    try {
                            pgCommand.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(52, 'DEVELOPMENT')";
                            pgCommand.ExecuteNonQuery();
                            pgCommand.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(62, 'PRODUCTION')";
                            pgCommand.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 {
                            pgCommand.Dispose();
                            myTrans.Dispose();
                    }
        }
    }
    Public Shared Sub RunPgSqlTransaction(myConnString As String)
            Using pgConnection As New PgSqlConnection(myConnString)
                    pgConnection.Open()
                    ' Start a local transaction
                    Dim myTrans As PgSqlTransaction = pgConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted)
                    Dim pgCommand As PgSqlCommand = pgConnection.CreateCommand()
                    Try
                            pgCommand.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(52, 'DEVELOPMENT')"
                            pgCommand.ExecuteNonQuery()
                            pgCommand.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(62, 'PRODUCTION')"
                            pgCommand.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
                            pgCommand.Dispose()
                            myTrans.Dispose()
                    End Try
        End Using
    End Sub
    Inheritance Hierarchy

    System.Object
       System.MarshalByRefObject
          System.Data.Common.DbTransaction
             Devart.Common.DbTransactionBase
                Devart.Data.PostgreSql.PgSqlTransaction

    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