如何在vb.net中实现事务方式?

时间:2008-11-18 08:59:07

标签: vb.net ms-access transactions

我使用连接到MS-Access数据库的VB.net(200%)开发一个应用程序,我使用TableAdapter和Dataset连接到Access数据库文件。

我需要在保存到DB时实现一个简单的事务方法(commit,rollback)吗?

有没有办法在不需要使用内联SQL语句的情况下执行此操作?

谢谢,

2 个答案:

答案 0 :(得分:10)

当我读到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 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!

答案 1 :(得分:-2)

您可以在http://www.asp.net/learn/data-access/

找到大量数据访问教程