Represents a SQL transaction to be made in the DB2 database.
The following example creates a
DB2Connection and a
DB2Transaction. It also demonstrates how to use the
DB2Connection.BeginTransaction,
Commit, and
Rollback methods.
public static void RunDB2Transaction(string myConnString) {
using (DB2Connection db2Connection = new DB2Connection(myConnString)) {
db2Connection.Open();
// Start a local transaction
DB2Transaction myTrans = db2Connection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
DB2Command db2Command = db2Connection.CreateCommand();
try {
db2Command.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(52, 'DEVELOPMENT')";
db2Command.ExecuteNonQuery();
db2Command.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(62, 'PRODUCTION')";
db2Command.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 {
db2Command.Dispose();
myTrans.Dispose();
}
}
}
Public Shared Sub RunDB2Transaction(myConnString As String)
Using db2Connection As New DB2Connection(myConnString)
db2Connection.Open()
' Start a local transaction
Dim myTrans As DB2Transaction = db2Connection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted)
Dim db2Command As DB2Command = db2Connection.CreateCommand()
Try
db2Command.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(52, 'DEVELOPMENT')"
db2Command.ExecuteNonQuery()
db2Command.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(62, 'PRODUCTION')"
db2Command.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
db2Command.Dispose()
myTrans.Dispose()
End Try
End Using
End Sub
Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2