SqlCeTransaction.Rollback メソッド

トランザクションを保留状態からロールバックします。

名前空間:  System.Data.SqlServerCe
アセンブリ:  System.Data.SqlServerCe (System.Data.SqlServerCe.dll)

構文

'宣言
<SecurityTreatAsSafeAttribute> _
<SecurityCriticalAttribute> _
<SecurityPermissionAttribute(SecurityAction.Assert, UnmanagedCode := True)> _
Public Overrides Sub Rollback
'使用
Dim instance As SqlCeTransaction

instance.Rollback()
[SecurityTreatAsSafeAttribute]
[SecurityCriticalAttribute]
[SecurityPermissionAttribute(SecurityAction.Assert, UnmanagedCode = true)]
public override void Rollback()
[SecurityTreatAsSafeAttribute]
[SecurityCriticalAttribute]
[SecurityPermissionAttribute(SecurityAction::Assert, UnmanagedCode = true)]
public:
virtual void Rollback() override
[<SecurityTreatAsSafeAttribute>]
[<SecurityCriticalAttribute>]
[<SecurityPermissionAttribute(SecurityAction.Assert, UnmanagedCode = true)>]
abstract Rollback : unit -> unit 
[<SecurityTreatAsSafeAttribute>]
[<SecurityCriticalAttribute>]
[<SecurityPermissionAttribute(SecurityAction.Assert, UnmanagedCode = true)>]
override Rollback : unit -> unit 
public override function Rollback()

実装

IDbTransaction.Rollback()

例外

例外 条件
Exception

トランザクションをコミットしようとしているときにエラーが発生しました。

InvalidOperationException

トランザクションは既にコミットされているかロールバックされています。

または

接続が解除されています。

説明

トランザクションは、保留中の状態 (BeginTransaction の呼び出し後、Commit の呼び出し前) だけからロールバックできます。

使用例

SqlCeConnection および SqlCeTransaction を作成する例を次に示します。BeginTransactionCommit、Rollback の各メソッドの使い方も示します。

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();
}

関連項目

参照

SqlCeTransaction クラス

System.Data.SqlServerCe 名前空間

Commit

BeginTransaction