Condividi tramite


SqlCeTransaction.Rollback Method

Note: This namespace, class, or member is supported only in version 1.1 of the .NET Framework.

Rolls back a transaction from a pending state.

  [Visual Basic]
  Public Overridable Sub Rollback() Implements _
   IDbTransaction.Rollback

  [C#]
  public virtual void Rollback();
[C++]
public: virtual void Rollback();
[JScript]
public function Rollback();

Implements

IDbTransaction.Rollback

Exceptions

Exception Type Condition
Exception An error occured while trying to commit the transaction.
InvalidOperationException The transaction has already been committed or rolled back.

-or-

The connection is broken.

Remarks

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

Example

[Visual Basic, C#] The following example creates a SqlCeConnection and a SqlCeTransaction. It also demonstrates how to use the BeginTransaction, a Commit, and Rollback methods.

  [Visual Basic] 
Public Sub RunSqlCeTransaction(myConnString As String)
   Dim myConnection As New SqlCeConnection(myConnString)
   myConnection.Open()
   
   Dim myCommand As New SqlCeCommand()
   Dim myTrans As SqlCeTransaction
   
   ' Start a local transaction
   myTrans = myConnection.BeginTransaction()
   
   ' Must assign both transaction object and connection
   ' to Command object for a pending local transaction
   myCommand.Connection = myConnection
   myCommand.Transaction = myTrans
   
   Try
      myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')"
      myCommand.ExecuteNonQuery()
      myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')"
      myCommand.ExecuteNonQuery()
      myTrans.Commit()
   Catch 
      Try
         myTrans.Rollback()
      Catch e As SqlCeException
      ' Handle possible exception here
      End Try 
   Finally
      myConnection.Close()
   End Try
End Sub 

[C#] 
public void RunSqlCeTransaction(string myConnString) {
    SqlCeConnection myConnection = new SqlCeConnection(myConnString);
    myConnection.Open();

    SqlCeCommand myCommand = new SqlCeCommand();
    SqlCeTransaction myTrans;

    // Start a local transaction
    myTrans = myConnection.BeginTransaction();
    
    // Must assign both transaction object and connection
    // to Command object for a pending local transaction
    myCommand.Connection = myConnection;
    myCommand.Transaction = myTrans;

    try {
        myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
        myCommand.ExecuteNonQuery();
        myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
        myCommand.ExecuteNonQuery();
        myTrans.Commit();
    }
    catch(Exception) {
        try {
            myTrans.Rollback();
        }
        catch (SqlCeException) {
            // Handle possible exception here
        }
    }
    finally {
        myConnection.Close();
    }
}

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: .NET Compact Framework

.NET Framework Security:

See Also

SqlCeTransaction Class | SqlCeTransaction Members | System.Data.SqlServerCe Namespace | Commit | BeginTransaction

Syntax based on .NET Framework version 1.1.
Documentation version 1.1.1.

Send comments on this topic.

© Microsoft Corporation. All rights reserved.