SqlTransaction 类

定义

表示将在 SQL Server 数据库中进行的 Transact-SQL 事务。 此类不能被继承。

public ref class SqlTransaction sealed : System::Data::Common::DbTransaction
public ref class SqlTransaction sealed : MarshalByRefObject, IDisposable, System::Data::IDbTransaction
public sealed class SqlTransaction : System.Data.Common.DbTransaction
public sealed class SqlTransaction : MarshalByRefObject, IDisposable, System.Data.IDbTransaction
type SqlTransaction = class
    inherit DbTransaction
type SqlTransaction = class
    inherit MarshalByRefObject
    interface IDbTransaction
    interface IDisposable
Public NotInheritable Class SqlTransaction
Inherits DbTransaction
Public NotInheritable Class SqlTransaction
Inherits MarshalByRefObject
Implements IDbTransaction, IDisposable
继承
SqlTransaction
继承
继承
SqlTransaction
实现

示例

以下示例创建 SqlConnectionSqlTransaction。 它还演示如何使用 BeginTransactionCommitRollback 方法。 事务在发生任何错误时回滚,或者在未首先提交的情况下释放事务。 Try / Catch 错误处理用于在尝试提交或回滚事务时处理任何错误。

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

注解

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

注意

Try / Catch 提交或回滚 SqlTransaction时,应始终使用异常处理。 如果连接终止或事务已在服务器上回滚,则 和 Rollback 都会Commit生成 InvalidOperationException

有关SQL Server事务的详细信息,请参阅显式事务高效事务编码

属性

Connection

获取与该事务关联的 SqlConnection 对象,或者如果该事务不再有效,则为 null

DbConnection

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

(继承自 DbTransaction)
IsolationLevel

为该事务指定 IsolationLevel

SupportsSavepoints

获取一个值,该值指示此 DbTransaction 实例是否支持数据库保存点。 如果为 false,则 SaveAsync(String, CancellationToken)RollbackAsync(String, CancellationToken)ReleaseAsync(String, CancellationToken) 方法及其同步对应项应引发 NotSupportedException

(继承自 DbTransaction)

方法

Commit()

提交数据库事务。

CommitAsync(CancellationToken)

异步提交数据库事务。

(继承自 DbTransaction)
CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。

(继承自 MarshalByRefObject)
Dispose()

释放由该对象占用的资源。

Dispose()

释放 DbTransaction 使用的非托管资源。

(继承自 DbTransaction)
Dispose(Boolean)

释放由 DbTransaction 占用的非托管资源,还可以另外再释放托管资源。

(继承自 DbTransaction)
DisposeAsync()

异步处置事务对象。

(继承自 DbTransaction)
Equals(Object)

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

(继承自 Object)
GetHashCode()

作为默认哈希函数。

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

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

(继承自 MarshalByRefObject)
GetType()

获取当前实例的 Type

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

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

(继承自 MarshalByRefObject)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
MemberwiseClone(Boolean)

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

(继承自 MarshalByRefObject)
Release(String)

销毁以前在当前事务中定义的保存点。 这允许系统在事务结束之前回收一些资源。

(继承自 DbTransaction)
ReleaseAsync(String, CancellationToken)

销毁以前在当前事务中定义的保存点。 这允许系统在事务结束之前回收一些资源。

(继承自 DbTransaction)
Rollback()

从挂起状态回滚事务。

Rollback(String)

从挂起状态回滚事务,并指定事务或保存点名称。

RollbackAsync(CancellationToken)

从挂起状态异步回滚事务。

(继承自 DbTransaction)
RollbackAsync(String, CancellationToken)

回滚在建立指定保存点后执行的所有命令。

(继承自 DbTransaction)
Save(String)

在事务中创建保存点(它可用于回滚事务的一部分),并指定保存点名称。

SaveAsync(String, CancellationToken)

在事务中创建保存点。 这样,在建立保存点后执行的所有命令都将被回滚,并将事务状态还原到保存点时的状态。

(继承自 DbTransaction)
ToString()

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

(继承自 Object)

显式接口实现

IDbTransaction.Connection

获取与事务关联的 DbConnection 对象,或空引用(如果该事务不再有效)。

(继承自 DbTransaction)

适用于

另请参阅