public void RunDB2Transaction(string myConnString) { DB2Connection db2Connection = new DB2Connection(myConnString); db2Connection.Open(); DB2Command db2Command = new DB2Command(); DB2Transaction myTrans; // Start a local transaction myTrans = db2Connection.BeginTransaction(); // Assign transaction object for a pending local transaction db2Command.Transaction = myTrans; try { db2Command.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(50, 'DEVELOPMENT')"; db2Command.ExecuteNonQuery(); db2Command.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(60, '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 { db2Connection.Close(); } }
Public Sub RunDB2Transaction(myConnString As String) Dim db2Connection As New DB2Connection(myConnString) db2Connection.Open() Dim db2Command As New DB2Command() Dim myTrans As DB2Transaction ' Start a local transaction myTrans = db2Connection.BeginTransaction() ' Assign transaction object for a pending local transaction db2Command.Transaction = myTrans Try db2Command.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(50, 'DEVELOPMENT')" db2Command.ExecuteNonQuery() db2Command.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(60, '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 db2Connection.Close() End Try 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