SqlTransaction.Rollback 方法

定义

从挂起状态回滚事务。

重载

Rollback()

从挂起状态回滚事务。

Rollback(String)

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

Rollback()

从挂起状态回滚事务。

public:
 override void Rollback();
public:
 virtual void Rollback();
public override void Rollback ();
public void Rollback ();
override this.Rollback : unit -> unit
abstract member Rollback : unit -> unit
override this.Rollback : unit -> unit
Public Overrides Sub Rollback ()
Public Sub Rollback ()

实现

例外

在尝试提交事务时出错。

已提交或回滚事务。

连接已断开。

示例

以下示例创建 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

注解

方法 Rollback 等效于 Transact-SQL ROLLBACK TRANSACTION 语句。 有关详细信息,请参阅 ROLLBACK TRANSACTION (Transact-SQL)

事务只能在调用 Commit) 之前BeginTransaction从挂起状态 (回滚。 如果事务在之前释放 CommitRollback 被调用,则会回滚该事务。

注意

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

有关SQL Server事务的详细信息,请参阅事务 (Transact-SQL)

另请参阅

适用于

Rollback(String)

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

public:
 void Rollback(System::String ^ transactionName);
public void Rollback (string transactionName);
override this.Rollback : string -> unit
member this.Rollback : string -> unit
Public Sub Rollback (transactionName As String)

参数

transactionName
String

要回滚的事务的名称,或要回滚到的保存点的名称。

例外

未指定事务名称。

已提交或回滚事务。

连接已断开。

示例

以下示例创建 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("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("SampleTransaction");
            }
            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("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("Exception Type: {0}", ex.GetType())
            Console.WriteLine("  Message: {0}", ex.Message)

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

            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

注解

方法 Rollback 等效于 Transact-SQL ROLLBACK TRANSACTION 语句。 有关详细信息,请参阅 事务 (Transact-SQL)

事务只能在调用 Commit) 之前BeginTransaction从挂起状态 (回滚。 如果事务在或Rollback调用之前Commit释放,则会回滚该事务。

注意

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

有关SQL Server事务的详细信息,请参阅事务 (Transact-SQL)

另请参阅

适用于