ADO. NET-level transactions

Source: Internet
Author: User


View Code
 
1 string sqlcon = "server =.; database = webSystem; uid = sa; pwd = ";
2 SqlConnection myConnection = new SqlConnection (sqlcon); // create a database connection object
3 myConnection. Open ();
4
5 // start a transaction
6 SqlTransaction sqltrans = myConnection. BeginTransaction ();
7
8 // create a command for the transaction
9 SqlCommand cmd = new SqlCommand ();
10 cmd. Connection = myConnection;
11 cmd. Transaction = sqltrans;
12 try
13 {
14 cmd. CommandText = "update dbo. Sys_LoginUser set UserName = 'allen' where UID = '19 '";
15 cmd. ExecuteNonQuery ();
16 cmd. CommandText = "update dbo. Sys_LoginUser set URealName = 'wankui 'where UID = '19 '";
17 cmd. ExecuteNonQuery ();
18 sqltrans. Commit (); // submit. The commit () method of SqlTransaction must be used to successfully complete a transaction.
19 Response. Write ("two data modified successfully ");
20
21}
22 catch (Exception ex)
23 {
24 sqltrans. Rollback (); // if an error occurs, roll back
25 Response. Write (ex );
26}
27 finally
28 {
29 myConnection. Close (); // Close the database connection
30}
 


From fangyangwa

Related Article

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.