OdbcConnection.BeginTransaction 메서드

정의

데이터 소스에서 트랜잭션을 시작합니다.Starts a transaction at the data source.

오버로드

BeginTransaction()

데이터 소스에서 트랜잭션을 시작합니다.Starts a transaction at the data source.

BeginTransaction(IsolationLevel)

지정된 IsolationLevel 값을 사용하여 데이터 소스에서 트랜잭션을 시작합니다.Starts a transaction at the data source with the specified IsolationLevel value.

BeginTransaction()

데이터 소스에서 트랜잭션을 시작합니다.Starts a transaction at the data source.

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

반환

OdbcTransaction

새 트랜잭션을 나타내는 개체입니다.An object representing the new transaction.

예외

트랜잭션이 현재 활성화되어 있는 경우.A transaction is currently active. 병렬 트랜잭션은 지원되지 않습니다.Parallel transactions are not supported.

예제

다음 예제에서는 및를 만듭니다 OdbcConnection OdbcTransaction .The following example creates an OdbcConnection and an OdbcTransaction. 사용 하는 방법을 보여 줍니다 합니다 BeginTransaction, Commit, 및 Rollback 메서드.It also demonstrates how to use the BeginTransaction, Commit, and Rollback methods.

public static void ExecuteTransaction(string connectionString)
{
    using (OdbcConnection connection =
               new OdbcConnection(connectionString))
    {
        OdbcCommand command = new OdbcCommand();
        OdbcTransaction transaction = null;

        // Set the Connection to the new OdbcConnection.
        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 OdbcConnection(connectionString)
        Dim command As New OdbcCommand()
        Dim transaction As OdbcTransaction

        ' Set the Connection to the new OdbcConnection.
        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

설명

트랜잭션을 커밋하거나 롤백하려면 또는 메서드를 명시적으로 사용 해야 합니다 Commit Rollback .To commit or roll back the transaction, you must explicitly use the Commit or Rollback methods.

ODBC 트랜잭션 관리 모델에 대 한 .NET Framework Data Provider이 올바르게 수행 되도록 하려면 데이터 원본에서 제공 하는 것과 같은 다른 트랜잭션 관리 모델을 사용 하지 마십시오.To make sure that the .NET Framework Data Provider for ODBC transaction management model performs correctly, avoid using other transaction management models, such as those provided by the data source.

참고

격리 수준을 지정 하지 않으면 사용 되는 드라이버에 의해 격리 수준이 결정 됩니다.If you do not specify an isolation level, the isolation level will be determined by the driver being used. 메서드를 사용 하 여 격리 수준을 지정 하려면 BeginTransaction 매개 변수를 사용 하는 오버 로드를 사용 합니다 isolevel .To specify an isolation level with the BeginTransaction method, use the overload that takes the isolevel parameter.

적용 대상

BeginTransaction(IsolationLevel)

지정된 IsolationLevel 값을 사용하여 데이터 소스에서 트랜잭션을 시작합니다.Starts a transaction at the data source with the specified IsolationLevel value.

public:
 System::Data::Odbc::OdbcTransaction ^ BeginTransaction(System::Data::IsolationLevel isolevel);
public System.Data.Odbc.OdbcTransaction BeginTransaction (System.Data.IsolationLevel isolevel);
override this.BeginTransaction : System.Data.IsolationLevel -> System.Data.Odbc.OdbcTransaction
member this.BeginTransaction : System.Data.IsolationLevel -> System.Data.Odbc.OdbcTransaction
Public Function BeginTransaction (isolevel As IsolationLevel) As OdbcTransaction

매개 변수

isolevel
IsolationLevel

이 연결에 대한 트랜잭션 격리 수준입니다.The transaction isolation level for this connection. 격리 수준을 지정하지 않으면 드라이버의 기본 격리 수준이 사용됩니다.If you do not specify an isolation level, the default isolation level for the driver is used.

반환

OdbcTransaction

새 트랜잭션을 나타내는 개체입니다.An object representing the new transaction.

예외

트랜잭션이 현재 활성화되어 있는 경우.A transaction is currently active. 병렬 트랜잭션은 지원되지 않습니다.Parallel transactions are not supported.

예제

다음 예제에서는 및를 만듭니다 OdbcConnection OdbcTransaction .The following example creates an OdbcConnection and an OdbcTransaction. 사용 하는 방법을 보여 줍니다 합니다 BeginTransaction, Commit, 및 Rollback 메서드.It also demonstrates how to use the BeginTransaction, Commit, and Rollback methods.

public static void ExecuteTransaction(string connectionString)
{
    using (OdbcConnection connection =
               new OdbcConnection(connectionString))
    {
        OdbcCommand command = new OdbcCommand();
        OdbcTransaction transaction = null;

        // Set the Connection to the new OdbcConnection.
        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 OdbcConnection(connectionString)
        Dim command As New OdbcCommand()
        Dim transaction As OdbcTransaction

        ' Set the Connection to the new OdbcConnection.
        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

설명

트랜잭션을 커밋하거나 롤백하려면 또는 메서드를 명시적으로 사용 해야 합니다 Commit Rollback .To commit or roll back the transaction, you must explicitly use the Commit or Rollback methods.

ODBC 트랜잭션 관리 모델에 대 한 .NET Framework Data Provider이 올바르게 수행 되도록 하려면 데이터 원본에서 제공 하는 것과 같은 다른 트랜잭션 관리 모델을 사용 하지 마십시오.To make sure that the .NET Framework Data Provider for ODBC transaction management model performs correctly, avoid using other transaction management models, such as those provided by the data source.

적용 대상