Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Commit Method
Collapse the table of content
Expand the table of content

SqlCeTransaction.Commit Method

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

Commits the database transaction.

[Visual Basic]
Public Overridable Sub Commit() Implements IDbTransaction.Commit
[C#]
public virtual void Commit();
[C++]
public: virtual void Commit();
[JScript]
public function Commit();

Implements

IDbTransaction.Commit

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.

Example

[Visual Basic, C#] The following example creates a SqlCeConnection and a SqlCeTransaction. It also 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

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

Show:
© 2015 Microsoft