Share via


SqlCeConnection.BeginTransaction Method (IsolationLevel)

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

Begins a database transaction with the current IsolationLevel value.

  [Visual Basic]
  Overloads Public Function BeginTransaction( _
   ByVal 
  isolationLevel
   As IsolationLevel _
) As SqlCeTransaction
[C#]
public SqlCeTransaction BeginTransaction(IsolationLevel isolationLevel
);
[C++]
public: SqlCeTransaction* BeginTransaction(IsolationLevel isolationLevel
);
[JScript]
public function BeginTransaction(
   isolationLevel : IsolationLevel) : SqlCeTransaction;

Parameters

  • isolationLevel
    The transaction isolation level for this connection.

Return Value

An object representing the new transaction.

Exceptions

Exception Type Condition
InvalidOperationException Parallel transactions are not supported.

Remarks

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

Example

[Visual Basic, C#] The following example creates a SqlCeConnection and a SqlCeTransaction, and then demonstrates how to use the BeginTransaction, 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

SqlCeConnection Class | SqlCeConnection Members | System.Data.SqlServerCe Namespace | SqlCeConnection.BeginTransaction Overload List | Commit | Rollback

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

Send comments on this topic.

© Microsoft Corporation. All rights reserved.