Database component Hxj.data (19) (Transaction II)

Source: Internet
Author: User

The transaction has been updated, but also to download a new version, compatible with the previous version, expanded the Dbtrans method.

More concise than the transaction in section 17 (transaction). As follows

using (DbTrans trans = DbSession.Default.BeginTransaction())
{
     trans.Update<Products>(Products._.ProductName, "apple", Products._.ProductID == 1);
     trans.Update<Products>(Products._.ProductName, "egg", Products._.ProductID == 2);
     trans.Commit();
}

Generated sql:

Text:

UPDATE [Products] SET [ProductName]=@e6b783222bb34b98b56fc3012500a0d5
WHERE [Products].[ProductID] = @bdb38c8f8fab405d93231e1b2f19c1e2 

Parameters:

@e6b783222bb34b98b56fc3012500a0d5[String] = apple
@bdb38c8f8fab405d93231e1b2f19c1e2[Int32] = 1

Text:

UPDATE [Products] SET [ProductName]=@4184e5de4ed545d9bf3f0ba9a38ed6fa
WHERE [Products].[ProductID] = @f1da00f3628a43c2a9c66678e71049c7 

Parameters:

@4184e5de4ed545d9bf3f0ba9a38ed6fa[String] = egg 
@f1da00f3628a43c2a9c66678e71049c7[Int32] = 2

The execution effect is the same.

Try catch is also written in the same way.

DbTrans trans = DbSession.Default.BeginTransaction();
try
{
     trans.Update<Products>(Products._.ProductName, "apple", Products._.ProductID == 1);
     trans.Update<Products>(Products._.ProductName, "egg", Products._.ProductID == 2);
     trans.Commit();
}
catch
{
     trans.Rollback();
}
finally
{
     trans.Close();
}

A transaction in a component is not a distributed transaction, but a simple transaction encapsulation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.