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

OdbcConnection.BeginTransaction Method (IsolationLevel)

Starts a transaction at the data source with the specified IsolationLevel value.

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

'Declaration
Public Function BeginTransaction ( _
	isolevel As IsolationLevel _
) As OdbcTransaction

Parameters

isolevel
Type: System.Data.IsolationLevel

The transaction isolation level for this connection. If you do not specify an isolation level, the default isolation level for the driver is used.

Return Value

Type: System.Data.Odbc.OdbcTransaction
An object representing the new transaction.

ExceptionCondition
InvalidOperationException

A transaction is currently active. Parallel transactions are not supported.

To commit or roll back the transaction, you must explicitly use the Commit or Rollback methods.

To make sure that the .NET Framework Data Provider for ODBC transaction management model performs correctly, avoid using other transaction management models, such as those provided by the data source.

The following example creates an OdbcConnection and an OdbcTransaction. It also demonstrates how to use the BeginTransaction, Commit, and Rollback methods.

Public Sub ExecuteTransaction(ByVal connectionString As String)

    Using connection As New OdbcConnection(connectionString)
        Dim command As New OdbcCommand()
        Dim transaction As OdbcTransaction

        ' Set the Connection to the new OdbcConnection.
        command.Connection = connection

        ' Open the connection and execute the transaction. 
        Try
            connection.Open()

            ' Start a local transaction.
            transaction = connection.BeginTransaction()

            ' Assign transaction object for a pending local transaction.
            command.Connection = connection
            command.Transaction = transaction

            ' Execute the commands.
            command.CommandText = _
                "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')"
            command.ExecuteNonQuery()
            command.CommandText = _
                "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')"
            command.ExecuteNonQuery()

            ' Commit the transaction.
            transaction.Commit()
            Console.WriteLine("Both records are written to database.")

        Catch ex As Exception
            Console.WriteLine(ex.Message)
            ' Try to rollback the transaction 
            Try
                transaction.Rollback()

            Catch 
                ' Do nothing here; transaction is not active. 
            End Try 
        End Try 
        ' The connection is automatically closed when the 
        ' code exits the Using block. 
    End Using 
End Sub

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft