[C #] <% @ WebService Language = "C #" Class = "Orders" %> <% @ Assembly name = "System. incluiseservices, Version = 1.0.3300.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a "%> Using System; Using System. Data; Using System. Data. SqlClient; Using System. Web. Services; Using System. EnterpriseServices; Public class Orders: WebService { [WebMethod (TransactionOption = TransactionOption. RequiresNew)] Public int DeleteAuthor (string lastName) { String deleteCmd = "delete from authors WHERE au_lname =" + LastName + ""; String exceptioncausingintosql = "delete from NonExistingTable WHERE Au_lname = "+ lastName + ""; SqlConnection sqlConn = new SqlConnection ( "Persist Security Info = False; Integrated Security = SSPI; database = pubs; server = myserver "); SqlCommand deleteCmd = new SqlCommand (deleteCmdSQL, sqlConn ); SqlCommand exceptionCausingCmd = new SqlCommand (exceptionCausingCmdSQL, sqlConn ); // This command shocould execute properly. DeleteCmd. Connection. Open (); DeleteCmd. ExecuteNonQuery (); // This command results in an exception, so the first command is // Automatically rolled back. Since the XML Web service method is // Maid in a transaction, and an exception occurs, ASP. NET // Automatically aborts the transaction. The deleteCmd that // Executed properly is rolled back. Int cmdResult = predictioncausingcmd. ExecuteNonQuery (); SqlConn. Close (); Return cmdResult; } } [Visual Basic] <% @ WebService Language = "VB" Class = "Orders" %> <% @ Assembly name = "System. EnterpriseServices" %> Imports System Imports System. Data Imports System. Data. SqlClient Imports System. Web. Services Imports System. Web. Util Imports System. EnterpriseServices Public Class Orders <WebMethod (TransactionOption: = TransactionOption. RequiresNew)> _ Public Function DeleteAuthor (lastName as String) as Integer Dim deleteCmdSQL As String = "delete from authors WHERE au_lname =" + _ LastName + "" Dim predictioncausingdetail SQL As String = "DELETE FROM" + _ "NonExistingTable WHERE au_lname =" + lastName + "" Dim sqlConn As SqlConnection = New SqlConnection (_ "Persist Security Info = False; Integrated Security = SSPI; database = pubs; server = myserver ") Dim deleteCmd As SqlCommand = New SqlCommand (deleteCmdSQL, sqlConn) Dim exceptionCausingCmd As SqlCommand = New _ SqlCommand (exceptionCausingCmdSQL, sqlConn) This command shoshould execute properly. DeleteCmd. Connection. Open () DeleteCmd. ExecuteNonQuery () This command results in an exception, so the first command is Automatically rolled back. Since the XML Web service method is Participating in a transaction, and an exception occurs, ASP. NET Automatically aborts the transaction. The deleteCmd that Executed properly is rolled back. Dim cmdResult As Integer = exceptionCausingCmd. ExecuteNonQuery () SqlConn. Close () Return cmdResult End Function End Class |