visual - 如何在vb.net中實現交易方式?




visual vb net (2)

我使用連接到MS-Access數據庫的VB.net(200%)開發一個應用程序,我使用TableAdapter和Dataset連接到Access DB文件。

我需要實現一個簡單的事務方法(提交,回滾)保存到數據庫?

有沒有辦法做到這一點,而不需要使用內聯SQL語句?

謝謝,



當我讀到Microsoft Jet(Access數據庫引擎)支持事務。 所以你可以像這樣創建一個事務(來自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秒 ):

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)TransactionScopeDevX.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!





transactions