Database transaction operations are an important element of many data-driven applications. To start a transaction you must call method CtreeSqlConnection.BeginTransaction() to obtain a CtreeSqlTransaction object. Once you instantiated a CtreeSqlTransaction object, you can use it to commit or rollback transactions.
.NET VB Example
Sub CreateTable()
Dim conString As String = “User=ADMIN;Password=ADMIN;Database=ctreeSQL”
Dim hConnection As New CtreeSqlConnection(conString)
Dim hCommand As New CtreeSqlCommand(hConnection)
Dim hTransaction As CtreeSqlTransaction
hConnection.Open()
hTransaction = hConnection.BeginTransaction(IsolationLevel.ReadCommitted)
hCommand.Transaction = hTransaction
hCommand.CommandText = “create table tab1 (name char(20), age integer)”
hCommand.ExecuteNonQuery()
hTransaction.Commit()
hConnection.Close()
End Sub
.NET C# Example
void CreateTable()
{
String conString = “User=ADMIN;Password=ADMIN;Database=ctreeSQL”;
CtreeSqlConnection hConnection = new CtreeSqlConnection(conString);
CtreeSqlCommand hCommand = new CtreeSqlCommand(hConnection);
CtreeSqlTransaction hTransaction;
hConnection.Open();
hTransaction = hConnection.BeginTransaction(IsolationLevel.ReadCommitted);
hCommand.Transaction = hTransaction;
hCommand.CommandText = “create table tab1 (name (char(20), age integer)”;
hCommand.ExecuteNonQuery();
hTransaction.Commit();
hConnection.Close();
}
.NET C++ Example
void CreateTable()
{
String* conString = S“User=ADMIN;Password=ADMIN;Database=ctreeSQL”;
CtreeSqlConnection* hConnection = new CtreeSqlConnection(conString);
CtreeSqlCommand* hCommand = new CtreeSqlCommand(hConnection);
CtreeSqlTransaction* hTransaction;
hConnection->Open();
hTransaction = hConnection->BeginTransaction(IsolationLevel::ReadCommitted);
hCommand->Transaction = hTransaction;
hCommand->CommandText = “create table tab1 (name (char(20), age integer)”;
hCommand->ExecuteNonQuery();
hTransaction->Commit();
hConnection->Close();
}
.NET Delphi Example
procedure CreateTable
var
conString : String;
hConnection : CtreeSqlConnection;
hCommand : CtreeSqlCommand;
hTransaction : CtreeSqlTransaction;
begin
conString := 'User=ADMIN;Password=ADMIN;Database=ctreeSQL';
hConnection := CtreeSqlConnection.Create(conString);
hCommand := CtreeSqlCommand(hConnection);
hCommand.Transaction = hTransaction;
hConnection.Open();
hTransaction = hConnection.BeginTransaction(IsolationLevel.ReadCommitted);
hCommand.CommandRText := 'create table tab1 (name (char(20), age integer)';
hCommand.ExecuteNonQuery();
hTransaction.Commit();
hConnection.Close();
end;
When the CtreeSqlTransaction object is created, by calling the BeginTransaction() method of CtreeSqlConnection object, you can optionally specify the transaction isolation level. The standard transaction isolation levels as defined by current SQL standards are as follows: