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

  1. /*=====================================================================
  2. File:      TxObj.cs
  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. using System;
  22. using System.Data;
  23. using System.Data.SqlClient;
  24. using System.Reflection;
  25. using System.Windows.Forms;
  26. using System.EnterpriseServices;
  27. using System.Runtime.InteropServices;
  28. using System.Runtime.Serialization;
  29.  
  30. // the ApplicationName attribute specifies the name of the
  31. // COM+ Application that will hold assembly components
  32. [assembly: ApplicationName("TxDemoSvr")]
  33.  
  34. // the ApplicationActivation.ActivationOption attribute specifies 
  35. // where assembly components are loaded on activation
  36. // Library : components run in the creator's process
  37. // Server : components run in a system process, dllhost.exe
  38. [assembly: ApplicationActivation(ActivationOption.Library)]
  39.  
  40. // AssemblyKeyFile specifies the name of the strong key
  41. // that will be used to sign the assembly.
  42. // The .snk file was generated with sn.exe
  43. #if BuildBat  // Used when building from the command line
  44. [assembly: AssemblyKeyFile("TxDemoSvrCS.snk")]
  45. #else         // Used when building from within Visual Studio .NET
  46. [assembly: AssemblyKeyFile("..\\..\\TxDemoSvrCS.snk")]
  47. #endif
  48.  
  49. namespace TxDemoServerCS
  50. {
  51.     // This TxDemoSvr class is going to be hosted within COM+
  52.     // The transaction attribute enables us to do transactions within this class.
  53.     // Requires means that this class needs a transaction, this will be a new 
  54.     // transaction if there isn't one present already, 
  55.     // otherwise it will take part in the existing transaction.
  56.     [Transaction(TransactionOption.Required)] 
  57.     public class TxDemoSvr : ServicedComponent
  58.     {
  59.         private SqlConnection database;
  60.  
  61.         // First the SQLConnection needs to be setup.
  62.         private SqlConnection Database        
  63.         {
  64.             get
  65.             {
  66.                 if (database == null) database = new SqlConnection(
  67.                     "server=(local)\\NetSDK;Integrated Security=SSPI;database=TxDemoDB");
  68.                 return database;
  69.             }
  70.         }
  71.  
  72.         // This method returns the "current value" 
  73.         public int GetCurrentValue()
  74.         {
  75.             int currentValue = 0;
  76.             try
  77.             {            
  78.                 Database.Open();
  79.                 SqlCommand sqlCommand = new SqlCommand("select * from currentValue", 
  80.                     Database);
  81.                 SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
  82.                 sqlDataReader.Read();
  83.                 currentValue = (int)sqlDataReader["currentValue"];
  84.             }
  85.             catch(Exception e)
  86.             {
  87.                 throw new CurrentValueNotReadException(e);
  88.             }
  89.             finally
  90.             {
  91.                 Database.Close();
  92.             }
  93.             return currentValue ;
  94.         }
  95.  
  96.         // This method tries to update the "current value", first it updates the 
  97.         // database, then it checks if the value is allowed, if not it throws an 
  98.         // COMException so the [AutoComplete()] Attribute automatically does a 
  99.         // rollback of the transaction.
  100.         [AutoComplete]
  101.         public void AutoCompletePost(int newValue)
  102.         {
  103.             try
  104.             {
  105.                 Database.Open();
  106.                 SqlCommand sqlCommand = new SqlCommand(@"
  107.                                                                                                     UPDATE currentValue 
  108.                                                                                                     SET currentValue=@currentValue
  109.                                                                                                 ",Database);
  110.                 sqlCommand.Parameters.Add("@currentValue",SqlDbType.Int,4);
  111.                 sqlCommand.Parameters["@currentValue"].Value = newValue;
  112.                 try 
  113.                 {
  114.                     sqlCommand.ExecuteNonQuery();
  115.                 }
  116.                 catch(Exception e)
  117.                 {
  118.                     throw new DatabaseExecutionException(e);
  119.                 }
  120.             
  121.                 if (newValue < 0 || newValue > 10)
  122.                 {
  123.                     MessageBox.Show("About to abort the transaction because the new value (" 
  124.                         + newValue + ") is either <0 or >10");
  125.                     throw new ValueOutOfRangeException(newValue);
  126.                 }
  127.                 else
  128.                 {
  129.                     MessageBox.Show("About to commit the transaction");
  130.                 }
  131.             }
  132.             finally 
  133.             {
  134.                 Database.Close();
  135.             }
  136.         }
  137.  
  138.         // This method tries to update the "current value", first it updates the 
  139.         // database, then it checks if the value is allowed, if not it calls 
  140.         // ContextUtil.SetAbort to rollback the transaction.
  141.         public void Post(int newValue)
  142.     {
  143.             try
  144.             {
  145.                 Database.Open();
  146.                 SqlCommand sqlCommand = new SqlCommand(@"
  147.                                                                                                     UPDATE currentValue 
  148.                                                                                                     SET currentValue=@currentValue
  149.                                                                                                 ",Database);
  150.                 sqlCommand.Parameters.Add("@currentValue",SqlDbType.Int,4);
  151.                 sqlCommand.Parameters["@currentValue"].Value = newValue;
  152.                 try 
  153.                 {
  154.                     sqlCommand.ExecuteNonQuery();
  155.                 }
  156.                 catch(Exception e)
  157.                 {
  158.                     throw new DatabaseExecutionException(e);
  159.                 }
  160.  
  161.                 if (newValue<0 || newValue>10)
  162.                 {
  163.                     MessageBox.Show("About to abort the transaction because the new value (" 
  164.                         + newValue + ") is either <0 or >10");
  165.                     ContextUtil.SetAbort();
  166.                 }
  167.                 else
  168.                 {
  169.                     MessageBox.Show("About to commit the transaction");
  170.                     ContextUtil.SetComplete();
  171.                 }
  172.             } 
  173.             finally 
  174.             {
  175.                 Database.Close();
  176.             }
  177.         }
  178.     }
  179.     
  180.  
  181.         // These are the specialized exceptions this server can throw. 
  182.         // The constructor with the SerializationInfo and the StreamingContext are to 
  183.         // support serialization of the exception as it goes from COM+ to the client.
  184.         // More information on how to create custom exceptions can be found in the 
  185.         // Exception Handling Whitepaper.
  186.         
  187.         // CurrentValueNotReadException occurs when there is an error while reading the 
  188.         // current value from the database.
  189.         [Serializable]
  190.     public class CurrentValueNotReadException : Exception
  191.     {
  192.         public CurrentValueNotReadException(Exception ex) : 
  193.             base("Unable to get the current value from the database", ex)
  194.         {
  195.         }
  196.  
  197.         public CurrentValueNotReadException(SerializationInfo info, 
  198.             StreamingContext context) : base(info, context) 
  199.         {
  200.         }
  201.     }
  202.  
  203.         // DatabaseExecutionException occurs when there is an error while updating 
  204.         // the database
  205.         [Serializable]
  206.     public class DatabaseExecutionException : Exception
  207.     {
  208.         
  209.         public DatabaseExecutionException(Exception ex) : 
  210.                 base("Error while executing database commands", ex)
  211.         {
  212.         }
  213.         
  214.         public DatabaseExecutionException(SerializationInfo info, 
  215.             StreamingContext context) : base(info, context) 
  216.         {
  217.         }
  218.     }
  219.  
  220.         // AbortTransactionException occurs when an AutoComplete attribute is used 
  221.         // and the transaction needs to be aborted
  222.         [Serializable]
  223.     public class ValueOutOfRangeException : Exception
  224.     {
  225.         public ValueOutOfRangeException(int newValue) : 
  226.             base("the new value (" + newValue + ") is either <0 or >10")
  227.         {
  228.         }
  229.  
  230.         public ValueOutOfRangeException(SerializationInfo info, 
  231.             StreamingContext context) : base(info, context) 
  232.         {
  233.         }
  234.     }
  235.  
  236. }