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만듭니다. 사용 하는 방법을 보여 줍니다 합니다 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) 을 참조하세요.

트랜잭션이 롤백할 수 보류 상태에서 (후 BeginTransaction 가 호출 된 전에 Commit 라고). 트랜잭션이 삭제되거나 호출되기 전에 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만듭니다. 사용 하는 방법을 보여 줍니다 합니다 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)을 참조하세요.

트랜잭션이 롤백할 수 보류 상태에서 (후 BeginTransaction 가 호출 된 전에 Commit 라고). 트랜잭션이 이전에 삭제되거나 호출된 CommitRollback 경우 롤백됩니다.

참고

Try/Catch 예외 처리는 트랜잭션을 롤백할 때 항상 사용해야 합니다. 는 Rollback 연결이 종료되거나 트랜잭션이 이미 서버에서 롤백된 경우 을 생성 InvalidOperationException 합니다.

SQL Server 트랜잭션에 대한 자세한 내용은 트랜잭션(Transact-SQL)을 참조하세요.

추가 정보

적용 대상