다음을 통해 공유


OdbcTransaction 클래스

데이터 소스에서 만들어지는 SQL 트랜잭션을 나타냅니다. 이 클래스는 상속될 수 없습니다.

네임스페이스: System.Data.Odbc
어셈블리: System.Data(system.data.dll)

구문

‘선언
Public NotInheritable Class OdbcTransaction
    Inherits DbTransaction
‘사용 방법
Dim instance As OdbcTransaction
public sealed class OdbcTransaction : DbTransaction
public ref class OdbcTransaction sealed : public DbTransaction
public final class OdbcTransaction extends DbTransaction
public final class OdbcTransaction extends DbTransaction

설명

응용 프로그램은 OdbcConnection 개체의 BeginTransaction을 호출함으로써 OdbcTransaction 개체를 만듭니다. 트랜잭션과 관련된 모든 후속 작업(예를 들어, 트랜잭션 커밋이나 중단)은 OdbcTransaction 개체에서 수행됩니다.

예제

다음 예제에서는 OdbcConnectionOdbcTransaction을 만듭니다. BeginTransaction, CommitRollback 메서드를 사용하는 방법도 보여 줍니다.

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
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.
    }

상속 계층 구조

System.Object
   System.MarshalByRefObject
     System.Data.Common.DbTransaction
      System.Data.Odbc.OdbcTransaction

스레드로부터의 안전성

이 형식의 모든 public static(Visual Basic의 경우 Shared) 멤버는 스레드로부터 안전합니다. 인터페이스 멤버는 스레드로부터 안전하지 않습니다.

플랫폼

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1에서 지원

참고 항목

참조

OdbcTransaction 멤버
System.Data.Odbc 네임스페이스
OdbcDataAdapter 클래스
OdbcConnection 클래스

기타 리소스

트랜잭션 수행