Export (0) Print
Expand All

SqlCeTransaction.Rollback Method

Rolls back a transaction from a pending state.

Namespace:  System.Data.SqlServerCe
Assembly:  System.Data.SqlServerCe (in System.Data.SqlServerCe.dll)

[SecurityTreatAsSafeAttribute]
[SecurityCriticalAttribute]
[SecurityPermissionAttribute(SecurityAction.Assert, UnmanagedCode = true)]
public override void Rollback()

Implements

IDbTransaction.Rollback()

ExceptionCondition
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.

The transaction can only be rolled back from a pending state (after BeginTransaction has been called, but before Commit is called).

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

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();
}


Show:
© 2014 Microsoft