IDbTransaction 接口

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

**命名空间:**System.Data
**程序集:**System.Data(在 system.data.dll 中)

语法

声明
Public Interface IDbTransaction
    Inherits IDisposable
用法
Dim instance As IDbTransaction
public interface IDbTransaction : IDisposable
public interface class IDbTransaction : IDisposable
public interface IDbTransaction extends IDisposable
public interface IDbTransaction extends IDisposable

备注

IDbTransaction 接口允许继承类实现 Transaction 类,Transaction 类表示要在数据源上执行的事务。有关 Transaction 类的更多信息,请参见 执行事务。有关实现 .NET Framework 数据提供程序的更多信息,请参见 Implementing a .NET Framework Data Provider

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

继承 IDbTransaction 的类必须实现继承的成员,并通常会定义附加成员来添加提供程序特定的功能。例如,IDbTransaction 接口定义 Commit 方法。然后,OleDbTransaction 类继承此属性,还定义 Begin 方法。

给实现者的说明 为提高 .NET Framework 数据提供程序之间的一致性,请命名 Prv Transaction 窗体中的继承类,其中 Prv 是给予特定 .NET Framework 数据提供程序命名空间中的所有类的统一前缀。例如,Sql 是 System.Data.SqlClient 命名空间中 SqlTransaction 类的前缀。

示例

下面的示例创建派生类 SqlConnectionSqlTransaction 的实例。此示例还演示如何使用 BeginTransactionCommitRollback 等方法。

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("SampleTransaction")

        ' 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
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("SampleTransaction");

        // 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);
            }
        }
    }
}

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

IDbTransaction 成员
System.Data 命名空间