Export (0) Print
Expand All

OracleConnection.BeginTransaction Method ()

.NET Framework 1.1
Note: This namespace, class, or member is supported only in version 1.1 of the .NET Framework.

Begins a transaction at the database.

[Visual Basic]
Overloads Public Function BeginTransaction() As OracleTransaction
[C#]
public OracleTransaction BeginTransaction();
[C++]
public: OracleTransaction* BeginTransaction();
[JScript]
public function BeginTransaction() : OracleTransaction;

Return Value

An object representing the new transaction.

Exceptions

Exception Type Condition
InvalidOperationException Parallel transactions are not supported.

Remarks

To commit or roll back the transaction, you must explicitly use the Commit or Rollback methods.

To ensure that the .NET Framework Data Provider for Oracle transaction management model performs correctly, avoid using other transaction management models, such as those provided by the database.

Note   If you do not specify an isolation level, the default isolation level is used. To specify an isolation level with the BeginTransaction method, use the overload that takes the il parameter.

Example

[Visual Basic, C#, C++] The following example creates an OracleConnection and an OracleTransaction. It also demonstrates how to use the BeginTransaction, Commit, and Rollback methods.

[Visual Basic] 
Public Sub RunOracleTransaction(myConnString As String)
    Dim myConnection As New OracleConnection(myConnString)
    myConnection.Open()
    
    Dim myCommand As OracleCommand = myConnection.CreateCommand()
    Dim myTrans As OracleTransaction
       
    ' 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 Dept (DeptNo, Dname, Loc) values (50, 'TECHNOLOGY', 'DENVER')"
        myCommand.ExecuteNonQuery()
        myCommand.CommandText = "INSERT INTO Dept (DeptNo, Dname, Loc) values (60, 'ENGINEERING', 'KANSAS CITY')"
        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

[C#] 
public void RunOracleTransaction(string myConnString)
{
   OracleConnection myConnection = new OracleConnection(myConnString);
   myConnection.Open();

   OracleCommand myCommand = myConnection.CreateCommand();
   OracleTransaction 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 Dept (DeptNo, Dname, Loc) values (50, 'TECHNOLOGY', 'DENVER')";
     myCommand.ExecuteNonQuery();
     myCommand.CommandText = "INSERT INTO Dept (DeptNo, Dname, Loc) values (60, 'ENGINEERING', 'KANSAS CITY')";
     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();
   }
}

[C++] 
public:
 void RunOracleTransaction(String* myConnString)
 {
    OracleConnection* myConnection = new OracleConnection(myConnString);
    myConnection->Open();

    OracleCommand* myCommand = myConnection->CreateCommand();
    OracleTransaction* myTrans;

    // Start a local transaction
    myTrans = myConnection->BeginTransaction(IsolationLevel::ReadCommitted);
    // Assign transaction object for a pending local transaction
    myCommand->Transaction = myTrans;

    try
    {
      myCommand->CommandText = S"INSERT INTO Dept (DeptNo, Dname, Loc) values (50, 'TECHNOLOGY', 'DENVER')";
      myCommand->ExecuteNonQuery();
      myCommand->CommandText = S"INSERT INTO Dept (DeptNo, Dname, Loc) values (60, 'ENGINEERING', 'KANSAS CITY')";
      myCommand->ExecuteNonQuery();
      myTrans->Commit();
      Console::WriteLine(S"Both records are written to database.");
    }
    catch(Exception* e)
    {
      myTrans->Rollback();
      Console::WriteLine(e);
      Console::WriteLine(S"Neither record was written to database.");
    }
    __finally
    {
      myConnection->Close();
    }
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

OracleConnection Class | OracleConnection Members | System.Data.OracleClient Namespace | OracleConnection.BeginTransaction Overload List | Commit | Rollback

Show:
© 2014 Microsoft