OdbcTransaction 类

定义

表示要对数据源进行的 SQL 事务。 此类不能被继承。

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
实现

示例

以下示例创建一个 OdbcConnection 和一个 OdbcTransaction。 它还演示了如何使用BeginTransactionCommitRollback方法。

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

注解

应用程序通过调用OdbcTransactionBeginTransaction对象来创建OdbcConnection对象。 与事务关联的所有后续操作(例如,提交或中止事务)对对象执行 OdbcTransaction

属性

名称 说明
Connection

OdbcConnection获取与事务关联的对象,或者null如果事务不再有效。

DbConnection

在派生类中重写时,获取 DbConnection 与事务关联的对象。

(继承自 DbTransaction)
IsolationLevel

指定 IsolationLevel 此事务。

方法

名称 说明
Commit()

提交数据库事务。

CreateObjRef(Type)

创建一个对象,其中包含生成用于与远程对象通信的代理所需的所有相关信息。

(继承自 MarshalByRefObject)
Dispose()

释放由 <a0/a0> 使用的非托管资源。

(继承自 DbTransaction)
Dispose(Boolean)

释放由托管资源使用 DbTransaction 的非托管资源,并选择性地释放托管资源。

(继承自 DbTransaction)
Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetLifetimeService()
已过时.

检索控制此实例的生存期策略的当前生存期服务对象。

(继承自 MarshalByRefObject)
GetType()

获取当前实例的 Type

(继承自 Object)
InitializeLifetimeService()
已过时.

获取生存期服务对象来控制此实例的生存期策略。

(继承自 MarshalByRefObject)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。

(继承自 MarshalByRefObject)
Rollback()

从挂起状态回滚事务。

ToString()

返回一个表示当前对象的字符串。

(继承自 Object)

显式接口实现

名称 说明
IDbTransaction.Connection

获取与 DbConnection 事务关联的对象;如果事务不再有效,则获取 null 引用。

(继承自 DbTransaction)
IDisposable.Dispose()

此 API 支持产品基础结构,不能在代码中直接使用。

释放类的 OdbcTransaction 当前实例使用的资源。

适用于

另请参阅