SqlCeConnection.BeginTransaction Method

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

Begins a database transaction.

Overload List

Begins a database transaction.

Supported only by the .NET Compact Framework.

[Visual Basic] Overloads Public Function BeginTransaction() As SqlCeTransaction

[C#] public SqlCeTransaction BeginTransaction();

[C++] public: SqlCeTransaction* BeginTransaction();

[JScript] public function BeginTransaction() : SqlCeTransaction;

Begins a database transaction with the current IsolationLevel value.

Supported only by the .NET Compact Framework.

[Visual Basic] Overloads Public Function BeginTransaction(IsolationLevel) As SqlCeTransaction

[C#] public SqlCeTransaction BeginTransaction(IsolationLevel);

[C++] public: SqlCeTransaction* BeginTransaction(IsolationLevel);

[JScript] public function BeginTransaction(IsolationLevel) : SqlCeTransaction;

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, C#] Note   This example shows how to use one of the overloaded versions of BeginTransaction. For other examples that might be available, see the individual overload topics.

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

See Also

SqlCeConnection Class | SqlCeConnection Members | System.Data.SqlServerCe Namespace

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

Send comments on this topic.

© Microsoft Corporation. All rights reserved.