dotConnect Universal Documentation
Devart.Data.Universal Namespace / UniConnection Class / BeginTransaction Method / BeginTransaction(IsolationLevel) Method
Isolation level, one of System.Data.IsolationLevel values.
Example

In This Topic
    BeginTransaction(IsolationLevel) Method
    In This Topic
    Begins a database transaction with the specified isolation level.
    Syntax
    'Declaration
     
    Public Overloads Shadows Function BeginTransaction( _
       ByVal il As IsolationLevel _
    ) As UniTransaction
    public new UniTransaction BeginTransaction( 
       IsolationLevel il
    )

    Parameters

    il
    Isolation level, one of System.Data.IsolationLevel values.

    Return Value

    An object representing the new transaction.
    Remarks
    To commit or roll back the transaction, you must explicitly use the Commit or Rollback methods.

    For the list of supported isolation levels refer to UniTransaction.IsolationLevel property.

    Example
    The following example creates a UniConnection and a UniTransaction. It also demonstrates how to use the 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(IsolationLevel.ReadCommitted);
      // Assign transaction object for a pending local transaction
      myCommand.Transaction = myTrans;
    
      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(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(IsolationLevel.ReadCommitted)
      ' Assign transaction object for a pending local transaction
      myCommand.Transaction = myTrans
    
      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
    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