IDbTransaction 接口

定义

表示在数据源中执行的事务,由访问关系数据库的 .NET 数据提供程序实现。

public interface class IDbTransaction : IDisposable
public interface IDbTransaction : IDisposable
type IDbTransaction = interface
    interface IDisposable
Public Interface IDbTransaction
Implements IDisposable
派生
实现

示例

以下示例创建派生类 SqlConnectionSqlTransaction和 的实例。 它还演示如何使用 BeginTransactionCommitRollback 方法。

private static void ExecuteSqlTransaction(string connectionString)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();

        SqlCommand command = connection.CreateCommand();
        SqlTransaction transaction;

        // Start a local transaction.
        transaction = connection.BeginTransaction();

        // Must assign both transaction object and connection
        // to Command object for a pending local transaction
        command.Connection = connection;
        command.Transaction = transaction;

        try
        {
            command.CommandText =
                "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
            command.ExecuteNonQuery();
            command.CommandText =
                "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
            command.ExecuteNonQuery();

            // Attempt to commit the transaction.
            transaction.Commit();
            Console.WriteLine("Both records are written to database.");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
            Console.WriteLine("  Message: {0}", ex.Message);

            // Attempt to roll back the transaction.
            try
            {
                transaction.Rollback();
            }
            catch (Exception ex2)
            {
                // This catch block will handle any errors that may have occurred
                // on the server that would cause the rollback to fail, such as
                // a closed connection.
                Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
                Console.WriteLine("  Message: {0}", ex2.Message);
            }
        }
    }
}
Private Sub ExecuteSqlTransaction(ByVal connectionString As String)
    Using connection As New SqlConnection(connectionString)
        connection.Open()

        Dim command As SqlCommand = connection.CreateCommand()
        Dim transaction As SqlTransaction

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

        ' Must assign both transaction object and connection
        ' to Command object for a pending local transaction.
        command.Connection = connection
        command.Transaction = transaction

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

            command.ExecuteNonQuery()

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

        Catch ex As Exception
            Console.WriteLine("Commit Exception Type: {0}", ex.GetType())
            Console.WriteLine("  Message: {0}", ex.Message)

            ' Attempt to roll back the transaction.
            Try
                transaction.Rollback()

            Catch ex2 As Exception
                ' This catch block will handle any errors that may have occurred
                ' on the server that would cause the rollback to fail, such as
                ' a closed connection.
                Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType())
                Console.WriteLine("  Message: {0}", ex2.Message)
            End Try
        End Try
    End Using
End Sub

注解

接口 IDbTransaction 允许继承类实现 Transaction 类,该类表示将在数据源中执行的事务。 有关事务类的详细信息,请参阅 事务和并发

应用程序不会直接创建 接口的 IDbTransaction 实例,而是创建继承 的类的 IDbTransaction实例。

继承 IDbTransaction 的类必须实现继承的成员,并且通常定义其他成员以添加特定于提供程序的功能。 例如, IDbTransaction 接口定义 Commit 方法。 类反过来 OleDbTransaction 会继承此属性,并定义 Begin 方法。

实施者说明

若要促进.NET Framework数据提供程序之间的一致性,请以 Transaction 的形式Prv命名继承类,其中 Prv 是给定给特定.NET Framework数据提供程序命名空间中所有类的统一前缀。 例如, Sql 是 命名空间中 类的SqlTransactionSystem.Data.SqlClient前缀。

属性

Connection

指定要与事务关联的 Connection 对象。

IsolationLevel

为该事务指定 IsolationLevel

方法

Commit()

提交数据库事务。

Dispose()

执行与释放或重置非托管资源关联的应用程序定义的任务。

(继承自 IDisposable)
Rollback()

从挂起状态回滚事务。

适用于