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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/298194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:55:05  来源:igfitidea点击:

How to implement transaction way in vb.net?

vb.netms-accesstransactions

提问by artur02

I develop one application using VB.net (200%) that connects to MS-Access Database, I use TableAdapter and Dataset for connection to the Access DB file.

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

I need to implement a simple transaction method (commit, rollback) in saving to the DB?

我需要在保存到数据库时实现一个简单的事务方法(提交、回滚)?

Is there a way to do that without the need to use inline SQL statement?

有没有办法在不需要使用内联 SQL 语句的情况下做到这一点?

Thanks,

谢谢,

回答by artur02

As I read Microsoft Jet (Access DB Engine) supports transactions. So you can create a transaction like this (example from CodeProject):

当我阅读 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();

An easier way is (example from 15 Seconds):

更简单的方法是(例如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;

}

You will need MSDTC running on your machine if you are using TransactionScope.

如果您使用TransactionScope ,您将需要在您的机器上运行 MSDTC 。

Unfortunately TableAdapter does not expose a connection property, so you need a workaround. So you need some workaround:

不幸的是TableAdapter 没有公开连接属性,所以你需要一个解决方法。所以你需要一些解决方法:

1) Reflection(example form CodeProject)

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();

Reflection can be very slow!

反射可能会很慢!

2) TransactionScope(example form DevX.com)

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();
   }

You will need MSDTC!

您将需要 MSDTC!

回答by user38123

You can find a bunch of data access tutorials at http://www.asp.net/learn/data-access/

你可以在http://www.asp.net/learn/data-access/找到一堆数据访问教程