Database component Hxj.data (18) (batch)

Source: Internet
Author: User

Batch processing is the submission of a script that is not executed immediately, but is submitted to a certain number.

Or the first example.

using (DbBatch batch = DbSession.Default.BeginBatchConnection())
{
     batch.Update<Products>(Products._.ProductName, "apple", Products._.ProductID == 1);
     batch.Update<Products>(Products._.ProductName, "pear", Products._.ProductID == 2);
     batch.Update<Products>(Products._.ProductName, "orange", Products._.ProductID == 3);
}

Generated sql:

Text:

UPDATE [Products] SET [ProductName]=@3d40ca14f83644f18269874c99e621a5 
WHERE [Products].[ProductID] = @50999b135fbf4f068f89bda7ab341ac3;
UPDATE [Products] SET [ProductName]=@fe1ea91dd6e14260813ea19abbc245eb
WHERE [Products].[ProductID] = @6bef4d78041549fa817c3363bd847d41;
UPDATE [Products] SET [ProductName]=@075f1b4639c446be88bfc33638b1f754
WHERE [Products].[ProductID] = @588a5481ff3d4ff58fc304293173b6ab; 

Parameters:

@3d40ca14f83644f18269874c99e621a5[String] = apple
@50999b135fbf4f068f89bda7ab341ac3[Int32] = 1
@fe1ea91dd6e14260813ea19abbc245eb[String] = pear 
@6bef4d78041549fa817c3363bd847d41[Int32] = 2
@075f1b4639c446be88bfc33638b1f754[String] = orange
@588a5481ff3d4ff58fc304293173b6ab[Int32] = 3

The default is 10 SQL execution once. can also be customized.

DbBatch batch = DbSession.Default.BeginBatchConnection(20)

This will set up 20 SQL execution once.

And you can set the internal transaction level.

DbBatchbatch = DbSession.Default.BeginBatchConnection(20, IsolationLevel.ReadCommitted)

can also be enforced:

using (DbBatch batch = DbSession.Default.BeginBatchConnection())
{
     batch.Update<Products>(Products._.ProductName, "apple", Products._.ProductID == 1);
     batch.Update<Products>(Products._.ProductName, "pear", Products._.ProductID == 2);
     batch.Execute();
     batch.Update<Products>(Products._.ProductName, "orange", Products._.ProductID == 3);
}

Perform batch. Execute (), the previous SQL script will be submitted first.

Generated sql:

Text:

UPDATE [Products] SET [ProductName]=@1ae514d0db3247acb196b8d55110a8e8
WHERE [Products].[ProductID] = @c694d46594324eec9db0d1e26a3ea499;
UPDATE [Products] SET [ProductName]=@4b1d4e07b6cc45d994e65c5eeac12619
WHERE [Products].[ProductID] = @58fa8ceaede745f0ac541d5c45574309;
Parameters: @1ae514d0db3247acb196b8d55110a8e8[String] = apple
@c694d46594324eec9db0d1e26a3ea499[Int32] = 1
@4b1d4e07b6cc45d994e65c5eeac12619[String] = pear
@58fa8ceaede745f0ac541d5c45574309[Int32] = 2

Text:

UPDATE [Products] SET [ProductName]=@c3a36b7a94754cd884ee27d4e42199a5
WHERE [Products].[ProductID] = @550ebc4788f54105ababe5d6bdc9cf2a;
Parameters: @c3a36b7a94754cd884ee27d4e42199a5[String] = orange
@550ebc4788f54105ababe5d6bdc9cf2a[Int32] = 3

See the output script executed two times.

Try catch is worded as follows:

DbBatch batch = DbSession.Default.BeginBatchConnection();
try
{
     batch.Update<Products>(Products._.ProductName, "apple1", Products._.ProductID == 1);
     batch.Update<Products>(Products._.ProductName, "pear1", Products._.ProductID == 2);
     batch.Update<Products>(Products._.ProductName, "orange1", Products._.ProductID == 3);
}
catch
{
     //do something
}
finally
{
     batch.Close();
}

The effect is the same as the first example.

Batch processing is also relatively simple.

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.