About. Net nested transaction

Source: Internet
Author: User

Connection. begintransaction ()

The transactions of this method cannot be nested.

Error: Message"Sqlconnection does not support parallel transactions. "String

You can use transactionscope:

Windows

Distributed Transaction Coordinator (Distributed Transaction Coordinator)

Service.

// Main. csusing system; using system. collections. generic; using system. LINQ; using system. text; using system. data. sqlclient; using system. transactions; namespace consoleapptesttrannest {class program {static void main (string [] ARGs) {try {using (transactionscope S = new transactionscope ()) {sqlconnection conn = new sqlconnection (@ "Data Source =. \ sql2008r2; database = test; user id = sa; Password = 123; integrate D SECURITY = no; "); Conn. open (); sqlcommand cmd = Conn. createcommand (); cmd. commandtext = "insert into AAAA values ('a1')"; cmd. executenonquery (); cmd. commandtext = "insert into AAAA values ('a1')"; cmd. executenonquery (); Conn. close (); subclass. operationsql (); S. complete () ;}} catch (system. exception e) {console. writeline (E. message);} console. writeline ("done"); console. readkey () ;}}// subclass. CSU Sing system; using system. collections. generic; using system. LINQ; using system. text; using system. data. sqlclient; using system. transactions; namespace consoleapptesttrannest {public class subclass {public static void operationsql () {try {using (transactionscope S = new transactionscope ()) {sqlconnection conn = new sqlconnection (@ "Data Source =. \ sql2008r2; database = test; user id = sa; Password = 123; integrat Ed SECURITY = no; "); If (conn. State! = System. data. connectionstate. open) Conn. open (); sqlcommand cmd = Conn. createcommand (); cmd. commandtext = "insert into AAAA values ('s1')"; cmd. executenonquery (); int I = 0, B = 1, C; C = B/I; cmd. commandtext = "insert into AAAA values ('s1')"; cmd. executenonquery (); Conn. close (); S. complete () ;}} catch (system. exception e) {console. writeline (E. stacktrace );}}}}

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.