다음을 통해 공유


SqlTransaction.Commit 메서드

정의

데이터베이스 트랜잭션을 커밋합니다.

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

구현

예외

트랜잭션을 커밋하는 동안 오류가 발생한 경우

트랜잭션이 이미 커밋 또는 롤백된 경우

또는

연결이 손상된 경우

예제

다음 예제에서는 및 를 SqlConnectionSqlTransaction만듭니다. 사용 하는 방법을 보여 줍니다 합니다 Commit, BeginTransaction, 및 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

설명

메서드는 Commit Transact-SQL COMMIT TRANSACTION 문과 동일합니다. 모든 수정 사항이 데이터베이스의 영구적인 부분이 되었기 때문에 커밋된 후에는 트랜잭션을 롤백할 수 없습니다. 자세한 내용은 COMMIT TRANSACTION(Transact-SQL) 을 참조하세요.

참고

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

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

적용 대상

추가 정보