トランザクション
トランザクションの確立
デフォルトでは、ADO.NET接続はアクティブなトランザクションを持ちません。 トランザクションは、接続のBeginTransaction メソッドを使用して明示的に開始される必要があります。これは、トランザクションの有効期間を制御するDbTransaction オブジェクトを返します。
トランザクションが作成されたら、接続上で実行されるすべての
コマンドのDbTransaction プロパティは、そのトランザクションオブジェクトに設定されている必要があります。
一度にアクティブにできるトランザクションは1つだけで、すべてのコマンドは
そのトランザクションの下で実行されなければなりません。
LakebaseConnection connection;
LakebaseCommand cmd = new LakebaseCommand(conn);
// This creates a new transaction and registers it with the command
DbTransaction tran = conn.BeginTransaction();
cmd.DbTransaction = tran;
// Both of these statements execute in the transaction
cmd.CommandText = "INSERT INTO ExampleTable (ExampleCol) VALUES ('ExampleVal1')";
cmd.ExecuteNonQuery();
cmd.CommandText = "INSERT INTO ExampleTable (ExampleCol) VALUES ('ExampleVal2')";
cmd.ExecuteNonQuery();
// This ends the current transaction and unregisters it from the command
tran.Commit();
cmd.DbTransaction = null;
トランザクションのコミットとロールバック
トランザクションオブジェクトは、トランザクションのコミットおよびロールバックに使用されます。
トランザクションは、トランザクションのCommit メソッドを使用してコミットすることができます。コミットは
トランザクション内のすべての変更を受け入れ、その変更が
一貫している場合受け入れを永続化します。
tran.Commit();
トランザクションは、トランザクションのRollback メソッドを使用してロールバックすることができます。
ロールバックするとトランザクションで実行されたすべての変更が取り消されます。
tran.Rollback();