Share via


SqlCeConnection.BeginTransaction 메서드 (IsolationLevel)

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

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

구문

‘선언
<SecurityTreatAsSafeAttribute> _
<SecurityCriticalAttribute> _
<SecurityPermissionAttribute(SecurityAction.Assert, UnmanagedCode := True)> _
Public Function BeginTransaction ( _
    isolationLevel As IsolationLevel _
) As SqlCeTransaction
‘사용 방법
Dim instance As SqlCeConnection
Dim isolationLevel As IsolationLevel
Dim returnValue As SqlCeTransaction

returnValue = instance.BeginTransaction(isolationLevel)
[SecurityTreatAsSafeAttribute]
[SecurityCriticalAttribute]
[SecurityPermissionAttribute(SecurityAction.Assert, UnmanagedCode = true)]
public SqlCeTransaction BeginTransaction(
    IsolationLevel isolationLevel
)
[SecurityTreatAsSafeAttribute]
[SecurityCriticalAttribute]
[SecurityPermissionAttribute(SecurityAction::Assert, UnmanagedCode = true)]
public:
SqlCeTransaction^ BeginTransaction(
    IsolationLevel isolationLevel
)
[<SecurityTreatAsSafeAttribute>]
[<SecurityCriticalAttribute>]
[<SecurityPermissionAttribute(SecurityAction.Assert, UnmanagedCode = true)>]
member BeginTransaction : 
        isolationLevel:IsolationLevel -> SqlCeTransaction 
public function BeginTransaction(
    isolationLevel : IsolationLevel
) : SqlCeTransaction

매개 변수

반환 값

유형: System.Data.SqlServerCe.SqlCeTransaction
새 트랜잭션을 나타내는 개체입니다.

주의

Commit 또는 Rollback 메서드를 사용하여 명시적으로 트랜잭션을 커밋하거나 롤백해야 합니다.

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

Dim conn As New SqlCeConnection("Data Source = AdventureWorks.sdf;")
conn.Open()

' Start a local transaction; SQL Mobile supports the following 
' isolation levels: ReadCommitted, RepeatableRead, Serializable
'
Dim tx As SqlCeTransaction = conn.BeginTransaction(IsolationLevel.ReadCommitted)

' By default, commands run in auto-commit mode; 
'
Dim cmd1 As SqlCeCommand = conn.CreateCommand()

' You may create multiple commands on the same connection
'
Dim cmd2 As SqlCeCommand = conn.CreateCommand()

' To enlist a command in a transaction, set the Transaction property
'
cmd1.Transaction = tx

Try
    cmd1.CommandText = "INSERT INTO FactSalesQuota " & _
        "(EmployeeKey, TimeKey, SalesAmountQuota) " & _
        "VALUES (2, 1158, 150000.00)"

    cmd1.ExecuteNonQuery()

    ' Auto-commited because cmd2 is not enlisted in a transaction
    '
    cmd2.CommandText = "INSERT INTO FactSalesQuota " & _
        "(EmployeeKey, TimeKey, SalesAmountQuota) " & _
        "VALUES (3, 1157, 15000.00)"

    cmd2.ExecuteNonQuery()

    ' Commit the changes to disk if everything above succeeded;
    ' Use Deferred mode for optimal performance; the changes will 
    ' be flashed to disk within the timespan specified in the 
    ' ConnectionString 'FLUSH INTERVAL' property; 
    '
    tx.Commit(CommitMode.Deferred)

    ' Alternatively, you could use:
    ' tx.Commit(CommitMode.Immediate);
    '
    ' or use default (Deferred) commit mode:
    ' tx.Commit()

Catch e As Exception
    ' Handle errors here
    '
    tx.Rollback()
Finally
    conn.Close()
End Try
SqlCeConnection conn = new SqlCeConnection("Data Source = AdventureWorks.sdf;");
conn.Open();

// Start a local transaction; SQL Mobile supports the following 
// isolation levels: ReadCommitted, RepeatableRead, Serializable
//
SqlCeTransaction tx = conn.BeginTransaction(IsolationLevel.ReadCommitted);

// By default, commands run in auto-commit mode; 
//
SqlCeCommand cmd1 = conn.CreateCommand();

// You may create multiple commands on the same connection
//
SqlCeCommand cmd2 = conn.CreateCommand();

// To enlist a command in a transaction, set the Transaction property
//
cmd1.Transaction = tx;

try
{
    cmd1.CommandText = "INSERT INTO FactSalesQuota " +
        "(EmployeeKey, TimeKey, SalesAmountQuota) " +
        "VALUES (2, 1158, 150000.00)";

    cmd1.ExecuteNonQuery();

    // Auto-commited because cmd2 is not enlisted in a transaction
    //
    cmd2.CommandText = "INSERT INTO FactSalesQuota " +
        "(EmployeeKey, TimeKey, SalesAmountQuota) " +
        "VALUES (3, 1157, 15000.00)";

    cmd2.ExecuteNonQuery();

    // Commit the changes to disk if everything above succeeded;
    // Use Deferred mode for optimal performance; the changes will 
    // be flashed to disk within the timespan specified in the 
    // ConnectionString 'FLUSH INTERVAL' property; 
    //
    tx.Commit(CommitMode.Deferred);

    // Alternatively, you could use:
    // tx.Commit(CommitMode.Immediate);
    //
    // or use default (Deferred) commit mode:
    // tx.Commit()
}
catch (Exception)
{
    // Handle errors here
    //
    tx.Rollback();
}
finally
{
    conn.Close();
}

참고 항목

참조

SqlCeConnection 클래스

BeginTransaction 오버로드

System.Data.SqlServerCe 네임스페이스

Commit

Rollback

관련 자료

격리 수준