home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / dotNETSDK / SETUP.EXE / netfxsd1.cab / FL_TxObj_vb________.3643236F_FC70_11D3_A536_0090278A1BB8 < prev    next >
Encoding:
Text File  |  2002-03-29  |  7.3 KB  |  194 lines

  1. '=====================================================================
  2. '  File:      TxObj.vb
  3. '
  4. '  Summary:   Server Code for .NET COM+ Transactions Sample
  5. '
  6. '---------------------------------------------------------------------
  7. '  This file is part of the Microsoft .NET Framework SDK Code Samples.
  8. '
  9. '  Copyright (C) Microsoft Corporation.  All rights reserved.
  10. '
  11. 'This source code is intended only as a supplement to Microsoft
  12. 'Development Tools and/or on-line documentation.  See these other
  13. 'materials for detailed information regarding Microsoft code samples.
  14. '
  15. 'THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  16. 'KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  17. 'IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  18. 'PARTICULAR PURPOSE.
  19. '=====================================================================
  20.  
  21. Imports System
  22. Imports System.Data
  23. Imports System.Data.SqlClient
  24. Imports System.Reflection
  25. Imports System.Windows.Forms
  26. Imports System.EnterpriseServices
  27. Imports System.Runtime.InteropServices
  28. Imports System.Runtime.Serialization
  29.  
  30.  
  31. ' the ApplicationName attribute specifies the name of the
  32. ' COM+ Application that will hold assembly components
  33. <Assembly: ApplicationName("TxDemoSvr")> 
  34.  
  35. ' the ApplicationActivation.ActivationOption attribute specifies 
  36. ' where assembly components are loaded on activation
  37. ' Library : components run in the creator's process
  38. ' Server : components run in a system process, dllhost.exe
  39. <Assembly: ApplicationActivation(ActivationOption.Library)> 
  40.  
  41. ' AssemblyKeyFile specifies the name of the strong key
  42. ' that will be used to sign the assembly.
  43. ' The .snk file was generated with sn.exe
  44. <Assembly: AssemblyKeyFile("TxDemoSvrVB.snk")> 
  45.  
  46. Namespace TxDemoServerVB
  47.  
  48.     'This TxDemoSvr class is going to be hosted within COM+
  49.     'The transaction attributes enables us to do transactions within the class.
  50.     <Transaction(TransactionOption.Required)> _
  51.     Public Class TxDemoSvr
  52.         Inherits ServicedComponent
  53.  
  54.         Private sqlConnection As SqlConnection
  55.  
  56.  
  57.         'First the SQLConnection needs to be setup.
  58.         Private ReadOnly Property Database as SqlConnection
  59.             Get
  60.                 if sqlConnection Is Nothing then sqlConnection = New sqlConnection _
  61.                     ("server=(local)\NetSDK;Integrated Security=SSPI;" & _
  62.                     "database=TxDemoDB")
  63.                 Database = sqlConnection
  64.             End Get
  65.         End Property
  66.  
  67.         'This method returns the "current value" 
  68.         Public Function GetCurrentValue() As Integer
  69.             Dim currentValue As Integer = 0
  70.             Try            
  71.                 Database.Open()
  72.                 Dim sqlCommand As New sqlCommand("select * from currentValue", _
  73.                     Database)
  74.                 Dim sqlDataReader As sqlDataReader = sqlCommand.ExecuteReader()
  75.                 sqlDataReader.Read()
  76.                 currentValue = CInt(sqlDataReader("currentValue"))
  77.                 sqlConnection.Close()
  78.             Catch ex as Exception               
  79.                 throw new CurrentValueNotReadException(ex)
  80.             Finally
  81.                 Database.Close()
  82.             End Try
  83.             GetCurrentValue = currentValue
  84.         End Function
  85.         
  86.         'This method tries to update the "current value", first it updates the
  87.         'database, then it checks if the value is allowed, if not it throws an 
  88.         'COMException so the <AutoComplete()> Attribute automatically does a 
  89.         'rollback of the transaction.
  90.         <AutoComplete()> _
  91.         Public Sub AutoCompletePost(ByVal newValue As Integer)
  92.             Try
  93.                 Database.Open()
  94.                 Dim sqlCommand As New sqlCommand("UPDATE currentValue SET " & _
  95.                     "currentValue=@currentValue", Database)
  96.                 sqlCommand.Parameters.Add("@currentValue", SqlDbType.Int, 4)
  97.                 sqlCommand.Parameters("@currentValue").Value = newValue
  98.                 Try
  99.                     sqlCommand.ExecuteNonQuery()
  100.                 Catch ex As Exception
  101.                     Throw New DatabaseExecutionException(ex)
  102.                 End Try
  103.             
  104.                 If newValue < 0 Or newValue > 10 Then
  105.                     MessageBox.Show(("About to abort the transaction because "& _
  106.                         "the new value (" & newValue & ") is either <0 or >10"))
  107.                     Throw New ValueOutOfRangeException(newValue)
  108.                 Else
  109.                     MessageBox.Show("About to commit the transaction")
  110.                 End If
  111.             Finally
  112.                 Database.Close()
  113.             End Try
  114.         End Sub
  115.  
  116.  
  117.         'This method tries to update the "current value", first it updates the 
  118.         'database, then it checks if the value is allowed, if not it calls 
  119.         'ContextUtil.SetAbort to rollback the transaction.
  120.         Public Sub Post(ByVal newValue As Integer)
  121.             Try            
  122.                 Database.Open()
  123.                 Dim sqlCommand As New sqlCommand("UPDATE currentValue SET " & _
  124.                     "currentValue=@currentValue", Database)
  125.                 sqlCommand.Parameters.Add("@currentValue", SqlDbType.Int, 4)
  126.                 sqlCommand.Parameters("@currentValue").Value = newValue
  127.                 Try
  128.                     sqlCommand.ExecuteNonQuery()
  129.                 Catch ex as Exception
  130.                     Throw New DatabaseExecutionException(ex)
  131.                 End Try
  132.                 If newValue < 0 Or newValue > 10 Then
  133.                     MessageBox.Show(("About to abort the transaction because "& _
  134.                         "the new value (" & newValue & ") is either <0 or >10"))
  135.                     ContextUtil.SetAbort()
  136.                 Else
  137.                     MessageBox.Show("About to commit the transaction")
  138.                     ContextUtil.SetComplete()
  139.                 End If
  140.             Finally
  141.                 Database.Close()
  142.             End Try
  143.         End Sub
  144.     End Class
  145.     
  146.     'These are the specialized exceptions this server can throw. 
  147.     'The constructor with the SerializationInfo and the StreamingContext 
  148.     'are to support serialization of the thread as it goes from COM+ to the 
  149.     'client.
  150.  
  151.     'CurrentValueNotReadException occurs when there is an error while reading 
  152.     'the current value from the database
  153.  
  154.     <Serializable()> _
  155.     Public Class CurrentValueNotReadException 
  156.         Inherits Exception
  157.         Sub New(ex as Exception)
  158.             MyBase.New("Unable to get the current value from the database", ex)
  159.         End Sub
  160.         
  161.         Sub New(info as SerializationInfo, context as StreamingContext)  
  162.             MyBase.New(info, context) 
  163.         End Sub
  164.     End Class
  165.  
  166.     'DatabaseExecutionException occurs when there is an error while updating 
  167.     'the database    
  168.     <Serializable()> _
  169.     Public Class DatabaseExecutionException 
  170.         Inherits Exception
  171.         Sub New(ex as Exception)
  172.             MyBase.New("Error while executing database commands", ex)
  173.         End Sub
  174.         
  175.         Sub New(info as SerializationInfo, context as StreamingContext)  
  176.             MyBase.New(info, context) 
  177.         End Sub
  178.     End Class
  179.     
  180.     'AbortTransactionException occurs when an AutoComplete attribute is used and 
  181.     'the transaction needs to be aborted
  182.     <Serializable()> _
  183.     Public Class ValueOutOfRangeException
  184.         Inherits Exception
  185.         Sub New(ByVal newValue As Integer)
  186.             MyBase.New("the new value (" & newValue & ") is either <0 or >10")
  187.         End Sub
  188.  
  189.         Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
  190.             MyBase.New(info, context)
  191.         End Sub
  192.     End Class
  193.  
  194. End Namespace