IDbTransaction 接口

定义

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

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

示例

以下示例创建派生类的实例, SqlConnection 以及 SqlTransaction。 它还演示了如何使用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命名空间中System.Data.SqlClient类的SqlTransaction前缀。

属性

Connection

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

IsolationLevel

为该事务指定 IsolationLevel

方法

Commit()

提交数据库事务。

Dispose()

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

(继承自 IDisposable)
Rollback()

从挂起状态回滚事务。

适用于