SqlCeConnection.BeginTransaction Method (IsolationLevel)

Begins a database transaction with the current IsolationLevel value.

Namespace: System.Data.SqlServerCe
Assembly: System.Data.SqlServerCe (in system.data.sqlserverce.dll)

Syntax

'Declaration
Public Function BeginTransaction ( _
    isolationLevel As IsolationLevel _
) As SqlCeTransaction
public SqlCeTransaction BeginTransaction (
    IsolationLevel isolationLevel
)
public:
SqlCeTransaction^ BeginTransaction (
    IsolationLevel isolationLevel
)
public SqlCeTransaction BeginTransaction (
    IsolationLevel isolationLevel
)
public function BeginTransaction (
    isolationLevel : IsolationLevel
) : SqlCeTransaction

Parameters

  • isolationLevel
    The transaction isolation level for this connection.

Return Value

An object representing the new transaction.

Remarks

You must explicitly commit or roll back the transaction using the Commit or Rollback method.

Example

The following example creates a SqlCeConnection and a SqlCeTransaction, and then demonstrates how to use the BeginTransaction, Commit, and Rollback methods.

Dim conn As New SqlCeConnection("Data Source = AdventureWorks.sdf;")
conn.Open()

' Start a local transaction; SQL Mobile supports the following 
' isolation levels: ReadCommitted, RepeatableRead, Serializable
'
Dim tx As SqlCeTransaction = conn.BeginTransaction(IsolationLevel.ReadCommitted)

' By default, commands run in auto-commit mode; 
'
Dim cmd1 As SqlCeCommand = conn.CreateCommand()

' You may create multiple commands on the same connection
'
Dim cmd2 As SqlCeCommand = conn.CreateCommand()

' To enlist a command in a transaction, set the Transaction property
'
cmd1.Transaction = tx

Try
    cmd1.CommandText = "INSERT INTO FactSalesQuota " & _
        "(EmployeeKey, TimeKey, SalesAmountQuota) " & _
        "VALUES (2, 1158, 150000.00)"

    cmd1.ExecuteNonQuery()

    ' Auto-commited because cmd2 is not enlisted in a transaction
    '
    cmd2.CommandText = "INSERT INTO FactSalesQuota " & _
        "(EmployeeKey, TimeKey, SalesAmountQuota) " & _
        "VALUES (3, 1157, 15000.00)"

    cmd2.ExecuteNonQuery()

    ' Commit the changes to disk if everything above succeeded;
    ' Use Deferred mode for optimal performance; the changes will 
    ' be flashed to disk within the timespan specified in the 
    ' ConnectionString 'FLUSH INTERVAL' property; 
    '
    tx.Commit(CommitMode.Deferred)

    ' Alternatively, you could use:
    ' tx.Commit(CommitMode.Immediate);
    '
    ' or use default (Deferred) commit mode:
    ' tx.Commit()

Catch e As Exception
    ' Handle errors here
    '
    tx.Rollback()
Finally
    conn.Close()
End Try
SqlCeConnection conn = new SqlCeConnection("Data Source = AdventureWorks.sdf;");
conn.Open();

// Start a local transaction; SQL Mobile supports the following 
// isolation levels: ReadCommitted, RepeatableRead, Serializable
//
SqlCeTransaction tx = conn.BeginTransaction(IsolationLevel.ReadCommitted);

// By default, commands run in auto-commit mode; 
//
SqlCeCommand cmd1 = conn.CreateCommand();

// You may create multiple commands on the same connection
//
SqlCeCommand cmd2 = conn.CreateCommand();

// To enlist a command in a transaction, set the Transaction property
//
cmd1.Transaction = tx;

try
{
    cmd1.CommandText = "INSERT INTO FactSalesQuota " +
        "(EmployeeKey, TimeKey, SalesAmountQuota) " +
        "VALUES (2, 1158, 150000.00)";

    cmd1.ExecuteNonQuery();

    // Auto-commited because cmd2 is not enlisted in a transaction
    //
    cmd2.CommandText = "INSERT INTO FactSalesQuota " +
        "(EmployeeKey, TimeKey, SalesAmountQuota) " +
        "VALUES (3, 1157, 15000.00)";

    cmd2.ExecuteNonQuery();

    // Commit the changes to disk if everything above succeeded;
    // Use Deferred mode for optimal performance; the changes will 
    // be flashed to disk within the timespan specified in the 
    // ConnectionString 'FLUSH INTERVAL' property; 
    //
    tx.Commit(CommitMode.Deferred);

    // Alternatively, you could use:
    // tx.Commit(CommitMode.Immediate);
    //
    // or use default (Deferred) commit mode:
    // tx.Commit()
}
catch (Exception)
{
    // Handle errors here
    //
    tx.Rollback();
}
finally
{
    conn.Close();
}

Thread Safety

Any public static (Shared in Microsoft Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Platforms

Development Platforms

Windows Vista, Windows Mobile 5.0, Windows XP Professional with Service Pack 2 (SP2), Windows Server 2003, Windows Mobile 2003 for Pocket PC, Windows CE 5.0
Version Information
.NET Framework and NET Compact Framework
Supported in 3.5
.NET Framework
Supported in 3.0
.NET Compact Framework and .Net Framework
Supported in 2.0

See Also

Reference

SqlCeConnection Class
SqlCeConnection Members
System.Data.SqlServerCe Namespace
Commit
Rollback

Other Resources

Isolation Levels