다음을 통해 공유


OleDbConnection.BeginTransaction 메서드

정의

데이터베이스 트랜잭션을 시작합니다.

오버로드

BeginTransaction()

현재 IsolationLevel 값을 사용하여 데이터베이스 트랜잭션을 시작합니다.

BeginTransaction(IsolationLevel)

지정된 격리 수준으로 데이터베이스 트랜잭션을 시작합니다.

BeginTransaction()

Source:
OleDbConnection.cs
Source:
OleDbConnection.cs
Source:
OleDbConnection.cs

현재 IsolationLevel 값을 사용하여 데이터베이스 트랜잭션을 시작합니다.

public:
 System::Data::OleDb::OleDbTransaction ^ BeginTransaction();
public System.Data.OleDb.OleDbTransaction BeginTransaction ();
override this.BeginTransaction : unit -> System.Data.OleDb.OleDbTransaction
member this.BeginTransaction : unit -> System.Data.OleDb.OleDbTransaction
Public Function BeginTransaction () As OleDbTransaction

반환

새 트랜잭션을 나타내는 개체입니다.

예외

병렬 트랜잭션은 지원되지 않습니다.

예제

다음 예제에서는 및 를 OleDbConnectionOleDbTransaction만듭니다. 사용 하는 방법을 보여 줍니다 합니다 BeginTransaction, Commit, 및 Rollback 메서드.

public void ExecuteTransaction(string connectionString)
{
    using (OleDbConnection connection =
               new OleDbConnection(connectionString))
    {
        OleDbCommand command = new OleDbCommand();
        OleDbTransaction transaction = null;

        // Set the Connection to the new OleDbConnection.
        command.Connection = connection;

        // Open the connection and execute the transaction.
        try
        {
            connection.Open();

            // Start a local transaction with ReadCommitted isolation level.
            transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);

            // Assign transaction object for a pending local transaction.
            command.Connection = connection;
            command.Transaction = transaction;

            // Execute the commands.
            command.CommandText =
                "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
            command.ExecuteNonQuery();
            command.CommandText =
                "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
            command.ExecuteNonQuery();

            // Commit the transaction.
            transaction.Commit();
            Console.WriteLine("Both records are written to database.");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            try
            {
                // Attempt to roll back the transaction.
                transaction.Rollback();
            }
            catch
            {
                // Do nothing here; transaction is not active.
            }
        }
        // The connection is automatically closed when the
        // code exits the using block.
    }
}
Public Sub ExecuteTransaction(ByVal connectionString As String)

    Using connection As New OleDbConnection(connectionString)
        Dim command As New OleDbCommand()
        Dim transaction As OleDbTransaction

        ' Set the Connection to the new OleDbConnection.
        command.Connection = connection

        ' Open the connection and execute the transaction.
        Try
            connection.Open()

            ' Start a local transaction with ReadCommitted isolation level.
            transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted)

            ' Assign transaction object for a pending local transaction.
            command.Connection = connection
            command.Transaction = transaction

            ' Execute the commands.
            command.CommandText = _
                "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')"
            command.ExecuteNonQuery()
            command.CommandText = _
                "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')"
            command.ExecuteNonQuery()

            ' Commit the transaction.
            transaction.Commit()
            Console.WriteLine("Both records are written to database.")

        Catch ex As Exception
            Console.WriteLine(ex.Message)
            ' Try to rollback the transaction
            Try
                    transaction.Rollback()

            Catch
                ' Do nothing here; transaction is not active.
            End Try
        End Try
        ' The connection is automatically closed when the
        ' code exits the Using block.
    End Using
End Sub

설명

또는 Rollback 메서드를 사용하여 Commit 트랜잭션을 명시적으로 커밋하거나 롤백해야 합니다. .NET Framework Data Provider for OLE DB 트랜잭션 관리 모델이 올바르게 수행되도록 하려면 데이터 원본에서 제공하는 것과 같은 다른 트랜잭션 관리 모델을 사용하지 않도록 합니다.

추가 정보

적용 대상

BeginTransaction(IsolationLevel)

Source:
OleDbConnection.cs
Source:
OleDbConnection.cs
Source:
OleDbConnection.cs

지정된 격리 수준으로 데이터베이스 트랜잭션을 시작합니다.

public:
 System::Data::OleDb::OleDbTransaction ^ BeginTransaction(System::Data::IsolationLevel isolationLevel);
public System.Data.OleDb.OleDbTransaction BeginTransaction (System.Data.IsolationLevel isolationLevel);
override this.BeginTransaction : System.Data.IsolationLevel -> System.Data.OleDb.OleDbTransaction
member this.BeginTransaction : System.Data.IsolationLevel -> System.Data.OleDb.OleDbTransaction
Public Function BeginTransaction (isolationLevel As IsolationLevel) As OleDbTransaction

매개 변수

isolationLevel
IsolationLevel

트랜잭션이 실행되어야 하는 격리 수준입니다.

반환

새 트랜잭션을 나타내는 개체입니다.

예외

병렬 트랜잭션은 지원되지 않습니다.

예제

다음 예제에서는 및 를 OleDbConnectionOleDbTransaction만듭니다. 또한 , , 및 Rollback 메서드를 Commit사용하는 BeginTransaction방법을 보여 줍니다.

public void ExecuteTransaction(string connectionString)
{
    using (OleDbConnection connection =
               new OleDbConnection(connectionString))
    {
        OleDbCommand command = new OleDbCommand();
        OleDbTransaction transaction = null;

        // Set the Connection to the new OleDbConnection.
        command.Connection = connection;

        // Open the connection and execute the transaction.
        try
        {
            connection.Open();

            // Start a local transaction
            transaction = connection.BeginTransaction();

            // Assign transaction object for a pending local transaction.
            command.Connection = connection;
            command.Transaction = transaction;

            // Execute the commands.
            command.CommandText =
                "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
            command.ExecuteNonQuery();
            command.CommandText =
                "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
            command.ExecuteNonQuery();

            // Commit the transaction.
            transaction.Commit();
            Console.WriteLine("Both records are written to database.");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            try
            {
                // Attempt to roll back the transaction.
                transaction.Rollback();
            }
            catch
            {
                // Do nothing here; transaction is not active.
            }
        }
        // The connection is automatically closed when the
        // code exits the using block.
    }
}
Public Sub ExecuteTransaction(ByVal connectionString As String)

    Using connection As New OleDbConnection(connectionString)
        Dim command As New OleDbCommand()
        Dim transaction As OleDbTransaction

        ' Set the Connection to the new OleDbConnection.
        command.Connection = connection

        ' Open the connection and execute the transaction.
        Try
            connection.Open()

            ' Start a local transaction.
            transaction = connection.BeginTransaction()

            ' Assign transaction object for a pending local transaction.
            command.Connection = connection
            command.Transaction = transaction

            ' Execute the commands.
            command.CommandText = _
                "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')"
            command.ExecuteNonQuery()
            command.CommandText = _
                "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')"
            command.ExecuteNonQuery()

            ' Commit the transaction.
            transaction.Commit()
            Console.WriteLine("Both records are written to database.")

        Catch ex As Exception
            Console.WriteLine(ex.Message)
            ' Try to rollback the transaction
            Try
                    transaction.Rollback()

            Catch
                ' Do nothing here; transaction is not active.
            End Try
        End Try
        ' The connection is automatically closed when the
        ' code exits the Using block.
    End Using
End Sub

설명

또는 Rollback 메서드를 사용하여 Commit 트랜잭션을 명시적으로 커밋하거나 롤백해야 합니다. .NET Framework Data Provider for OLE DB 트랜잭션 관리 모델이 올바르게 수행되도록 하려면 데이터 원본에서 제공하는 것과 같은 다른 트랜잭션 관리 모델을 사용하지 않도록 합니다.

참고

격리 수준을 지정하지 않으면 기본 공급자에 대한 기본 격리 수준이 사용됩니다. 메서드를 사용하여 격리 수준을 BeginTransaction 지정하려면 매개 변수를 사용하는 오버로드를 isolationLevel 사용합니다.

추가 정보

적용 대상