DB2Transaction.Commit Method
Commits the database transaction.
- Namespace:
IBM.Data.DB2
- Assembly:
IBM.Data.DB2
(inIBM.Data.DB2.dll
)
Syntax
[Visual Basic]
NotOverridable Public Sub Commit()
[C#]
public void Commit();
[C++]
public: __sealed void Commit();
[JScript]
public function Commit();
Exceptions
Exception type | Condition |
---|---|
Exception | An error occurred while trying to commit the transaction. |
InvalidOperationException | The transaction has already been committed
or rolled back. -or- The connection is broken. |
Example
[Visual Basic, C#] The following example creates a DB2®Connection and a DB2Transaction. It also demonstrates how to use the DB2Connection.BeginTransaction, 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()
' 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();
// 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();
}
}