Export (0) Print
Expand All

OracleConnection.BeginTransaction Method

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

Begins a transaction at the database.

Overload List

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;

Begins a transaction at the database with the specified IsolationLevel value.

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

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, C#, C++] Note   This example shows how to use one of the overloaded versions of BeginTransaction. For other examples that might be available, see the individual overload topics.
[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.

See Also

OracleConnection Class | OracleConnection Members | System.Data.OracleClient Namespace

Show:
© 2014 Microsoft