Represents an SQL transaction to be made at a database. This class cannot be inherited.
System.Object
System.MarshalByRefObject
System.Data.Common.DbTransaction
IBM.Data.DB2.DB2Transaction
[Visual Basic]
NotInheritable Public Class DB2Transaction
Inherits DbTransaction
Implements IDbTransaction, IDisposable
[C#]
public sealed class DB2Transaction : DbTransaction,
IDbTransaction, IDisposable
[C++]
public __gc __sealed class DB2Transaction : public
DbTransaction, IDbTransaction, IDisposable
[JScript]
public class DB2Transaction extends DbTransaction implements
IDbTransaction, IDisposable
The application creates an DB2Transaction object by calling BeginTransaction on the DB2Connection object. All subsequent operations associated with the transaction (for example, committing or aborting the transaction), are performed on the DB2Transaction object.
[Visual Basic, C#] The following example creates a DB2Connection and a DB2Transaction. It also demonstrates how to use the DB2Connection.BeginTransaction, DB2Transaction.Commit, and DB2Transaction.Rollback methods.
[Visual Basic]
Public Sub RunDB2Transaction(myConnString As String)
Dim myConnection As New DB2Connection(myConnString)
myConnection.Open()
Dim myCommand As New DB2Command()
Dim myTrans As DB2Transaction
' 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 org(
DEPTNUMB, DEPTNAME, MANAGER, DIVISION,LOCATION)
VALUES (100, 'Head Office', 160, 'Corporate', 'New York')"
myCommand.ExecuteNonQuery()
myCommand.CommandText = "Insert into org(
DEPTNUMB, DEPTNAME, MANAGER, DIVISION,LOCATION)
VALUES (101, 'New England', 50, 'Eastern', 'Boston')"
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 RunDB2Transaction(string myConnString)
{
DB2Connection myConnection = new DB2Connection(myConnString);
myConnection.Open();
DB2Command myCommand = new DB2Command();
DB2Transaction 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 org(
DEPTNUMB, DEPTNAME, MANAGER, DIVISION,LOCATION)
VALUES (100, 'Head Office', 160, 'Corporate', 'New York')";
myCommand.ExecuteNonQuery();
myCommand.CommandText = "Insert into org(
DEPTNUMB, DEPTNAME, MANAGER, DIVISION,LOCATION)
VALUES (101, 'New England', 50, 'Eastern', 'Boston')";
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();
}
}
Any public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Any instance members are not guaranteed to be thread safe.