0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ACCESSにADO.NETでアクセスする場合のトランザクション制御 #5

Last updated at Posted at 2015-06-18

接続型、非接続型が混在したトランザクション制御

接続型、非接続型が混在した場合のトランザクション制御

// 接続型の為の記述
OleDbCommand CMD = new OleDbCommand();
CMD.Connection = CONN;    // CONNはOleDbConnection。定義の記述を省略。

// 非接続型の為の記述
// DAはOleDbDataAdapter。定義の記述は省略。
OleDbCommandBuilder CB = new OleDbCommandBuilder(DA);
DA.InsertCommand = CB.GetInsertCommand();
DA.UpdateCommand = CB.GetUpdateCommand();
DA.DeleteCommand = CB.GetDeleteCommand();

// 共通
CONN.Open();

OleDbTransaction TRN = CONN.BeginTransaction(IsolationLevel.ReadCommitted);

// TransactionプロパティへのTransactionオブジェクトの代入
CMD.Transaction = TRN;    // 接続型の為の記述
DA.InsertCommand.Transaction = TRN;    // 非接続型の為の記述
DA.UpdateCommand.Transaction = TRN;    // 非接続型の為の記述
DA.DeleteCommand.Transaction = TRN;    // 非接続型の為の記述

try
{
    CMD.CommandText = "UPDATE 従業員テーブル xxx";
    CMD.ExecuteNonQuery();    // 接続型のSQL文発行

    DA.Update(DT);            // 非接続型のSQL文発行
    TRN.Commit();
}
catch (System.Exception)
{
    TRN.Rollback();
    textBox.Text = "エラーが発生したのでロールバックしました。" + ex.Message;
}
finally
{
    CONN.Close();
}

ACCESSにADO.NETでアクセスする場合のトランザクション制御 #1
非接続型のトランザクション制御
接続型のトランザクション制御
複数のDataTableの変更を同時にDBに反映する際のトランザクション制御
・接続型、非接続型が混在した更新のトランザクション制御
トランザクション制御に関する分かり難いエラーメッセージ

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?