Ado. Net two Transaction Processing Methods

Source: Internet
Author: User

The first type is sqltransaction-type transaction processing, which is based on the transaction processing on. NET code. Detailed access: http://www.cnblogs.com/windows/articles/1605636.htm

Advantages:

-Transactions cannot span multiple database connections.
-The transaction runs on the database connection layer. Therefore, you must maintain a database connection during the transaction process. Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Data;
Using system. Data. sqlclient;
Using system. Transactions;

Namespace DOTNET Data Access
{
Public partial class transaction processing: system. Web. UI. Page
{
String conn = "Data Source =.; initial catalog = test; Integrated Security = true ";
Sqlconnection con;
Protected void page_load (Object sender, eventargs E)
{

}

Protected void button#click (Object sender, eventargs E)
{

Con = new sqlconnection (conn );
Con. open ();
// Start a transaction.
Sqltransaction mytran = con. begintransaction ();

Sqlcommand mycom = new sqlcommand ();
Mycom. Connection = con;
Mycom. Transaction = mytran;
Try
{
Mycom. commandtext = "insert into fenye (value) values ('zz ')"; // This line is not inserted into the database
Mycom. executenonquery ();
Mycom. commandtext = "insert into fenye2 (value) values ('zz2 ')";
Mycom. executenonquery ();
Mytran. Commit ();
Response. Write ("executed successfully! 1 ");
}
Catch (exception ex)
{
Mytran. rollback ();
// Create an error message and return an exception
Response. Write (ex. tostring ());
Response. Write ("failed to write data to the database ");
}
Finally
{
Con. Close ();
}
}

Protected void button2_click (Object sender, eventargs E)
{
Using (transactionscope tscope = new transactionscope ())
{
Using (con = new sqlconnection (conn ))
{
Sqlcommand cmd = new sqlcommand ("insert into fenye (value) values ('zz ')", con );
Con. open ();
Cmd. executenonquery ();
}
Using (con = new sqlconnection (conn ))
{
Sqlcommand cmd = new sqlcommand ("insert into fenye2 (value) values ('zz2')", con );
Con. open ();
Cmd. executenonquery ();
}
Response. Write ("execution successful! 2 ");
Tscope. Complete ();
}
}
}
}

The second transaction uses the transactionscope class to compensate for the shortcomings of the first transaction. Detailed access: http://www.cnblogs.com/zhangpengshou/archive/2009/07/20/1527269.html

Note that before using the second type of transaction, first reference system. Transactions. dll, and then namespace reference. Make sure that the DTC service is enabled on your computer. Enable method access:

Http://it87.net/blog/post/2010/06/04/e69c8de58aa1e599a8c397c397c397e4b88ae79a84MSDTCe4b88de58fafe794a8e8a7a3e586b3e58a9ee6b395.aspx

The content is as follows:

MSDTC (Distributed Transaction Coordinator) coordinates transactions across multiple databases, message queues, file systems, and other resource managers. The service is named msdtc.exe. The process calls Microsoft Personal Web server and Microsoft SQL Server. This service is used to manage multiple servers. Location: control panel -- Management Tools -- service -- Distributed Transaction Coordinator
Dependency: Remote Procedure Call (RPC) and security accounts manager
Suggestion: Generally, it is not involved in the home computer, unless you enable the Message Queuing service, you can stop. Solution: 1. choose Windows Control Panel> Administrative Tools> services> Distributed Transaction Coordinator> Properties> Start 2. run "Net start MSDTC" under cmd to enable the Service.

NOTE: If Step 1 Distributed Transaction Coordinator cannot be started, it is because the log file is lost, re-create the log file, and then start it. To recreate the MSDTC log and restart the service, follow these steps:
(1) Click Start, click Run, Enter cmd, and then press OK ".
(2) Input MSDTC-resetlog (note that when running this command, do not execute the pending transaction)
(3) Enter Net start MSDTC and press Enter!

 

 

 

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.