SqlTransaction.Rollback 方法  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
从挂起状态回滚事务。
重载
| Rollback() | 从挂起状态回滚事务。 | 
| Rollback(String) | 从挂起状态回滚事务,并指定事务或保存点名称。 | 
Rollback()
从挂起状态回滚事务。
public:
 override void Rollback();public:
 virtual void Rollback();public override void Rollback();public void Rollback();override this.Rollback : unit -> unitabstract member Rollback : unit -> unit
override this.Rollback : unit -> unitPublic Overrides Sub Rollback ()Public Sub Rollback ()实现
例外
在尝试提交事务时出错。
示例
以下示例创建 SqlConnection 和 SqlTransaction。 它还演示如何使用 BeginTransaction、 Commit和 Rollback 方法。 事务在任何错误时都回滚。 
              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从挂起状态 (回滚。 如果事务在之前释放 Commit 或 Rollback 被调用,则会回滚该事务。
注意
              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 -> unitmember this.Rollback : string -> unitPublic Sub Rollback (transactionName As String)参数
- transactionName
- String
要回滚的事务的名称,或要回滚到的保存点的名称。
例外
未指定事务名称。
示例
以下示例创建 SqlConnection 和 SqlTransaction。 它还演示如何使用 BeginTransaction、 Commit和 Rollback 方法。 事务在任何错误时都回滚。 
              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) 。