Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Commit Method
Collapse the table of content
Expand the table of content

OracleTransaction.Commit Method

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

Commits the SQL database transaction.

[Visual Basic]
Public Overridable Sub Commit() Implements IDbTransaction.Commit
[C#]
public virtual void Commit();
[C++]
public: virtual void Commit();
[JScript]
public function Commit();

Implements

IDbTransaction.Commit

Exceptions

Exception Type Condition
Exception An error occurred while trying to commit the transaction.
InvalidOperationException The transaction has already been committed or rolled back.

-or-

The connection is broken.

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()
    ' 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();
   // 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();
    // 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

OracleTransaction Class | OracleTransaction Members | System.Data.OracleClient Namespace | BeginTransaction | Rollback

Show:
© 2015 Microsoft