See Also

OracleConnection Class  | OracleConnection Members  | Overload List  | OracleCommand Class  | OracleTransaction Class  | Commit Method  | Rollback Method

Language

Visual Basic

C#

Show All

See AlsoLanguagesDevart.Data.OracleSend comments on this topic.

BeginTransaction() Method

Devart.Data.Oracle Namespace > OracleConnection Class > BeginTransaction Method : BeginTransaction() Method

Begins a database transaction.

[Visual Basic]
Overloads Public Function BeginTransaction() As OracleTransaction
[C#]
public OracleTransaction BeginTransaction();

Return Type

An object representing the new transaction.

Remarks

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

Example

The following example creates a OracleConnection and a OracleTransaction. It also demonstrates how to use the BeginTransaction, Commit, and Rollback methods.

[C#] 

public void RunOracleTransaction(string myConnString) 

  OracleConnection myConnection = new OracleConnection(myConnString); 
  myConnection.Open(); 
 
  OracleCommand myCommand = new OracleCommand(); 
  myCommand.Connection = myConnection; 
  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 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(); 
  } 

[Visual Basic] 

Public Sub RunOracleTransaction(myConnString As String)
  Dim myConnection As New OracleConnection(myConnString)
  myConnection.Open()

  Dim myCommand As New OracleCommand()
  myCommand.Connection = myConnection
  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 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

See Also

OracleConnection Class  | OracleConnection Members  | Overload List  | OracleCommand Class  | OracleTransaction Class  | Commit Method  | Rollback Method

 

 


© 2002 - 2014 Devart. All Rights Reserved.