Send feedback on this topic.
Teradata.Client.Provider
BeginTransaction() Method
Example 



Teradata.Client.Provider Namespace > TdConnection Class > BeginTransaction Method : BeginTransaction() Method
Begins an explicit transaction. Applications call TdTransaction.Commit to commit the transaction or TdTransaction.Rollback to rollback the transaction. The Teradata session goes back into an auto-commit mode after the Commit or the Rollback method is called.
Syntax
'Declaration
 
Public Overloads Shadows Function BeginTransaction() As TdTransaction
'Usage
 
Dim instance As TdConnection
Dim value As TdTransaction
 
value = instance.BeginTransaction()
public new TdTransaction BeginTransaction()
public:
new TdTransaction^ BeginTransaction(); 

Return Value

TdTransaction representing the explicit transaction.
Example
The following example creates a TdConnection, a TdCommand and a TdTransaction class. It illustrates how to open an explicit transaction, perform DML operations and commit the result.
public void UpdateCustomerID(Int32 newId, Int32 oldId, String connectionString)
{
   TdConnection cn = null;
   TdTransaction tran = null;
   try
   {
       // Open a session to Teradata.
       cn = new TdConnection(connectionString);
       cn.Open();
            
       // Begin an explicit transaction.
       tran = cn.BeginTransaction();
             
       // Create a command to execute DML statements.  
       TdCommand cmd = cn.CreateCommand();
       cmd.Transaction = tran;
            
       // Update the customer table.
       cmd.CommandText = String.Format(“Update Customer Set CustomerID = ‘{0}’ "
where CustomerID = ‘{1}’”, newId, oldId);
       cmd.ExecuteNonQuery();
            
       // Update the order table.
       cmd.CommandText = String.Format(“Update Order Set CustomerID = ‘{0}’ “
                                      “where CusomerID = ‘{1}’”, newId, oldId);
       cmd.ExecuteNonQuery();
            
       // Commit the transaction.
       tran.Commit();
       tran.Dispose();
       tran = null;
            
       // Close the command.
       cmd.Dispose();
   }
   catch(TdException e)
   {
       if (null != tran)
       {
           tran.Rollback();
           tran.Dispose();
           tran = null;
       }
   }
   finally
   {
       if (null != cn)
       {
           cn.Close();
           cn = null;
       }
   }
}
Requirements

Target Platforms: Windows 7, Windows 8, Windows 8.1, Windows 10, Windows Server 2008, Windows Server 2008 R2, Windows Server 2012, Windows Server 2012 R2

See Also

Reference

TdConnection Class
TdConnection Members
Overload List