1

MS-Access データベースに接続する VB.net (200%) を使用して 1 つのアプリケーションを開発します。Access DB ファイルへの接続には TableAdapter と Dataset を使用します。

DBへの保存時に簡単なトランザクションメソッド(コミット、ロールバック)を実装する必要がありますか?

インライン SQL ステートメントを使用せずにそれを行う方法はありますか?

ありがとう、

4

2 に答える 2

11

私が読んだように、Microsoft Jet (Access DB Engine) はトランザクションをサポートしています。したがって、次のようなトランザクションを作成できます ( CodeProjectの例)。

      SqlConnection db = new SqlConnection("connstringhere");
      SqlTransaction transaction;

      db.Open();
      transaction = db.BeginTransaction();
      try 
      {
         new SqlCommand("INSERT INTO TransactionDemo " +
            "(Text) VALUES ('Row1');", db, transaction)
            .ExecuteNonQuery();
         new SqlCommand("INSERT INTO TransactionDemo " +
            "(Text) VALUES ('Row2');", db, transaction)
            .ExecuteNonQuery();
         new SqlCommand("INSERT INTO CrashMeNow VALUES " +
            "('Die', 'Die', 'Die');", db, transaction)
            .ExecuteNonQuery();
         transaction.Commit();
      } 
      catch (SqlException sqlError) 
      {
         transaction.Rollback();
      }
      db.Close();

簡単な方法は次のとおりです ( 15 Secondsの例):

bool IsConsistent = false;

using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope())

{

      SqlConnection cn = newSqlConnection(CONNECTION_STRING );

      string sql = "DELETE Categories";

      SqlCommand cmd = newSqlCommand(sql, cn);

      cn.Open();

      cmd.ExecuteNonQuery();

      cn.Close();

      //Based on this property the transaction will commit if

      //successful.  If it fails however, this property will

      //not be set and the transaction will not commit.

      ts.Consistent = IsConsistent;

}

TransactionScopeを使用している場合は、マシンで MSDTC を実行する必要があります。

残念ながら、TableAdapter は接続プロパティを公開していないため、回避策が必要です。したがって、いくつかの回避策が必要です。

1) リフレクション(サンプルフォームCodeProject )

conn = new SqlConnection(Properties.Settings.Default.NorthwindConnectionString);
conn.Open();
trans = conn.BeginTransaction();
1. 
public SqlDataAdapter GetAdapter(object tableAdapter)
{
    Type tableAdapterType = tableAdapter.GetType();
    SqlDataAdapter adapter = (SqlDataAdapter)tableAdapterType.GetProperty("Adapter", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(tableAdapter, null);
        return adapter;
}
2.
adapter.InsertCommand.Connection = trans.Connection;
adapter.UpdateCommand.Connection = trans.Connection;
adapter.DeleteCommand.Connection = trans.Connection;

3.
adapter.InsertCommand.Transaction = trans;
adapter.UpdateCommand.Transaction = trans;
adapter.DeleteCommand.Transaction = trans;

4. 
-

5. 
trans.commit();

反射は非常に遅くなる可能性があります。

2) TransactionScope ( DevX.com の例)

    CustomersDataSet.CustomersDataTable customers = new CustomersDataSet.CustomersDataTable();
   CustomersDataSetTableAdapters.CustomersTableAdapter tblAdap = new 
      CustomersDataSetTableAdapters.CustomersTableAdapter();
   using (TransactionScope txScope = new TransactionScope())
   {
       tblAdap.Fill(customers);
       customers.Rows[0]["ContactName"] = "Maria Velasquez";
       tblAdap.Update(customers);
       txScope.Complete();
   }

MSDTC が必要です。

于 2008-11-18T09:31:36.377 に答える
-2

http://www.asp.net/learn/data-access/で、一連のデータ アクセス チュートリアルを見つけることができます。

于 2008-11-18T09:30:01.667 に答える