OdbcTransaction 類別

定義

代表在資料來源進行的 SQL 異動。Represents an SQL transaction to be made at a data source. 此類別無法獲得繼承。This class cannot be inherited.

public ref class OdbcTransaction sealed : System::Data::Common::DbTransaction
public ref class OdbcTransaction sealed : MarshalByRefObject, IDisposable, System::Data::IDbTransaction
public sealed class OdbcTransaction : System.Data.Common.DbTransaction
public sealed class OdbcTransaction : MarshalByRefObject, IDisposable, System.Data.IDbTransaction
type OdbcTransaction = class
    inherit DbTransaction
type OdbcTransaction = class
    inherit MarshalByRefObject
    interface IDbTransaction
    interface IDisposable
Public NotInheritable Class OdbcTransaction
Inherits DbTransaction
Public NotInheritable Class OdbcTransaction
Inherits MarshalByRefObject
Implements IDbTransaction, IDisposable
繼承
OdbcTransaction
繼承
繼承
OdbcTransaction
實作

範例

下列範例會建立 OdbcConnectionOdbcTransactionThe following example creates an OdbcConnection and an OdbcTransaction. 它也會示範如何使用 BeginTransactionCommitRollback 方法。It also demonstrates how to use the BeginTransaction, Commit, and Rollback methods.

public static void ExecuteTransaction(string connectionString)
{
    using (OdbcConnection connection =
               new OdbcConnection(connectionString))
    {
        OdbcCommand command = new OdbcCommand();
        OdbcTransaction transaction = null;

        // 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 (Exception ex)
        {
            Console.WriteLine(ex.Message);
            try
            {
                // Attempt to roll back the transaction.
                transaction.Rollback();
            }
            catch
            {
                // Do nothing here; transaction is not active.
            }
        }
        // The connection is automatically closed when the
        // code exits the using block.
    }
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

備註

應用程式會藉 OdbcTransaction 由呼叫物件來建立物件 BeginTransaction OdbcConnectionThe application creates an OdbcTransaction object by calling BeginTransaction on the OdbcConnection object. 所有與交易相關聯的後續作業 (例如,認可或中止交易) 都是在物件上執行 OdbcTransactionAll subsequent operations associated with the transaction (for example, committing or aborting the transaction) are performed on the OdbcTransaction object.

屬性

Connection

取得與交易相關聯的 OdbcConnection 物件或 null (如果交易不再有效)。Gets the OdbcConnection object associated with the transaction, or null if the transaction is no longer valid.

DbConnection

當覆寫衍生類別時,取得交易相關聯的 DbConnection 物件。When overridden in a derived class, gets the DbConnection object associated with the transaction.

(繼承來源 DbTransaction)
IsolationLevel

指定適用於此交易的 IsolationLevelSpecifies the IsolationLevel for this transaction.

SupportsSavepoints

取得指出這個 DbTransaction 執行個體是否支援資料庫儲存點的值。Gets a value that indicates whether this DbTransaction instance supports database savepoints. 如果為 false,方法 SaveAsync(String, CancellationToken)RollbackAsync(String, CancellationToken)ReleaseAsync(String, CancellationToken) 以及各自的同步對應項目應該會擲回 NotSupportedExceptionIf false, the methods SaveAsync(String, CancellationToken), RollbackAsync(String, CancellationToken) and ReleaseAsync(String, CancellationToken) as well as their synchronous counterparts are expected to throw NotSupportedException.

(繼承來源 DbTransaction)

方法

Commit()

認可資料庫交易。Commits the database transaction.

CommitAsync(CancellationToken)

非同步地認可資料庫交易。Asynchronously commits the database transaction.

(繼承來源 DbTransaction)
CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(繼承來源 MarshalByRefObject)
Dispose()

釋放由 DbTransaction 使用的 Unmanaged 資源。Releases the unmanaged resources used by the DbTransaction.

(繼承來源 DbTransaction)
Dispose(Boolean)

釋放 DbTransaction 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。Releases the unmanaged resources used by the DbTransaction and optionally releases the managed resources.

(繼承來源 DbTransaction)
DisposeAsync()

非同步地處置交易物件。Asynchronously diposes the transaction object.

(繼承來源 DbTransaction)
Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetLifetimeService()
已過時。

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(繼承來源 MarshalByRefObject)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
InitializeLifetimeService()
已過時。

取得存留期服務物件,以控制這個執行個體的存留期原則。Obtains a lifetime service object to control the lifetime policy for this instance.

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。Creates a shallow copy of the current MarshalByRefObject object.

(繼承來源 MarshalByRefObject)
Release(String)

終結先前在目前交易中定義的儲存點。Destroys a savepoint previously defined in the current transaction. 這個動作可讓系統在交易結束前回收一些資源。This allows the system to reclaim some resources before the transaction ends.

(繼承來源 DbTransaction)
ReleaseAsync(String, CancellationToken)

終結先前在目前交易中定義的儲存點。Destroys a savepoint previously defined in the current transaction. 這個動作可讓系統在交易結束前回收一些資源。This allows the system to reclaim some resources before the transaction ends.

(繼承來源 DbTransaction)
Rollback()

從暫止狀態復原交易。Rolls back a transaction from a pending state.

Rollback(String)

復原在指定儲存點建立後執行的所有命令。Rolls back all commands that were executed after the specified savepoint was established.

(繼承來源 DbTransaction)
RollbackAsync(CancellationToken)

非同步地復原暫止狀態的交易。Asynchronously rolls back a transaction from a pending state.

(繼承來源 DbTransaction)
RollbackAsync(String, CancellationToken)

復原在指定儲存點建立後執行的所有命令。Rolls back all commands that were executed after the specified savepoint was established.

(繼承來源 DbTransaction)
Save(String)

在交易中建立儲存點。Creates a savepoint in the transaction. 這個動作會將您在儲存點建立後執行的所有命令回復,並將交易狀態還原到儲存點當時的狀態。This allows all commands that are executed after the savepoint was established to be rolled back, restoring the transaction state to what it was at the time of the savepoint.

(繼承來源 DbTransaction)
SaveAsync(String, CancellationToken)

在交易中建立儲存點。Creates a savepoint in the transaction. 這個動作會將您在儲存點建立後執行的所有命令回復,並將交易狀態還原到儲存點當時的狀態。This allows all commands that are executed after the savepoint was established to be rolled back, restoring the transaction state to what it was at the time of the savepoint.

(繼承來源 DbTransaction)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

明確介面實作

IDbTransaction.Connection

取得與交易相關聯的 DbConnection 物件,如果交易已不再有效,則為 Null 參考。Gets the DbConnection object associated with the transaction, or a null reference if the transaction is no longer valid.

(繼承來源 DbTransaction)
IDisposable.Dispose()

此 API 支援此產品基礎結構,但無法直接用於程式碼之中。

OdbcTransaction 類別的目前執行個體所使用的資源釋出。Releases the resources used by the current instance of the OdbcTransaction class.

適用於

另請參閱