(Original) Three hierarchical transactions in Web development

xiaoxiao2021-03-05  22

Many database operations need to be transactions, and the ASP.NET is roughly three levels: (1) Store procedure hierarchical transaction (2) ADO.NET hierarchical transaction (3) ASP.NET page Hierarchical transaction below: First establish the TRANTEST table, field ID (int), test (char) to set the primary key (using the primary key is the transaction test for repeated features) assumes that there is a record ID = 1, Test = 'Test' (1) CREATE Procedure TRAN1 AS Begin Tran Set XACT_ABORT ON Insert Into Tranteest (ID, TEST) VALUES (1, 'Test') INTO TRANTEST (ID, TEST) VALUES (2, 'Test') Commit TRAN Go SET XACT_ABORT ON means encountered an error Roll back now, you can also write create procedure tran1 as begin TRAN INSERT INTO TRANTEST (ID, TEST) VALUES (1, 'Test') IF (@@ Error <> 0) Rollback TRAN Else Begin Insert Into Trantest (ID, test) values ​​(2, 'test') if (@@ error <> 0) rollback tran else commit tran end GO (2) SqlConnection conn = new SqlConnection (System.Configuration.ConfigurationSettings.AppSettings [ "conn"]); SqlCommand CMD1 = New SQLCOMMAND ("INSERT INTO TRANTEST (ID, TEST) VALUES (1, 'Test')", CONN); SQLCOMMAND CMD2 = New SqlCommand ("INSERT INTO TRANTEST (ID, TEST) VALUES (1, 'Test') ", conn); conn.open (); sqltransaction tran = conn.begintransaction (); cmd1.transacti ON = Tran; cmd2.Transaction = TRAN; try {cmd1.executenonury (); cmd2.executenon query (); tran.Commit ();} catch (sqlexception except) {TRAN. ROLLBACK (); response.write (Except.MESSAGE );} Finally {conn.close ();} (3) Add reference system.EnterpriseServices.dll using system.EnterpriseServices; casually create a button, do the following: Try {Work1 (); Work2 (); contextUtil .SETCOMPLETE (); Catch (System.exception Except) {ContextUtil.Setabort ();

转载请注明原文地址:https://www.9cbs.com/read-37383.html

New Post(0)