dotConnect Universal Documentation
Devart.Data.Universal Namespace / UniTransaction Class
Members Example

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

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

    The correlation between UniConnection and UniTransaction is always 1:1.

    Example
    The following example creates a UniConnection and a UniTransaction. It also demonstrates how to use the UniConnection.BeginTransaction, Commit, and Rollback methods.
    public void RunUniTransaction(string myConnString)
    {
      UniConnection myConnection = new UniConnection(myConnString);
      myConnection.Open();
      UniCommand myCommand = new UniCommand();
      UniTransaction myTrans;
      // Start a local transaction
      myTrans = myConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
      // Assign transaction object for a pending local transaction
      myCommand.Transaction = myTrans;
      myCommand.Connection = myConnection;
      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 RunUniTransaction(ByVal myConnString As String)
      Dim myConnection As New UniConnection(myConnString)
      myConnection.Open()
      Dim myCommand As New UniCommand
      Dim myTrans As UniTransaction
      ' Start a local transaction
      myTrans = myConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted)
      ' Assign transaction object for a pending local transaction
      myCommand.Transaction = myTrans
      myCommand.Connection = myConnection
      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
    Inheritance Hierarchy

    System.Object
       System.MarshalByRefObject
          System.Data.Common.DbTransaction
             Devart.Common.DbTransactionBase
                Devart.Data.Universal.UniTransaction

    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