home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / dotNETSDK / SETUP.EXE / netfxsd1.cab / FL_Account_cs________.3643236F_FC70_11D3_A536_0090278A1BB8 < prev    next >
Encoding:
Text File  |  2002-05-06  |  6.5 KB  |  184 lines

  1. // ==============================================================================
  2. // Filename: Account.cs
  3. //
  4. // Summary:  C# implememtation of the account class for the bank sample
  5. // Classes:  Account.cs
  6. //
  7. // This file is part of the Microsoft COM+ 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 reagrding Microsoft code samples.
  14. //
  15. // THIS CODE AND INFORMATION IS 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.Reflection;
  23. using System.Runtime.InteropServices;
  24. using System.EnterpriseServices;
  25. using AccountComLib;
  26. using ADODB;
  27.  
  28. [assembly:AssemblyKeyFile("../common/testkey.snk")]
  29. [assembly:Description("CLR Com Services ServicedComponent Sample")]
  30. [assembly:ApplicationName("ServicedComponentApp")]
  31. [assembly:ApplicationActivation(ActivationOption.Library)]
  32.  
  33. namespace CSharpBank
  34. {
  35.     [
  36.         TransactionAttribute(TransactionOption.Required)
  37.     ]
  38.     public class Account : ServicedComponent, IAccount
  39.     {
  40.  
  41.         // F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++
  42.         //
  43.         // Function: Post
  44.         //
  45.         // Post() performs error handling for TruePost().  If Truepost() throws an exception,
  46.         // then Post() will call SetAbort() and pass the exception up to the caller.  Otherwise,
  47.         // Post() will simply call SetComplete() and return.
  48.         //
  49.         // Args:     lngAccountNo -    Account to be posted
  50.         //           lngAmount -     Amount to be posted
  51.         // Returns:  String -        Account Balance
  52.         //
  53.         // F-F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---
  54.  
  55.         public String Post (int lngAccountNo, int lngAmount)
  56.         {
  57.             String result = "";
  58.             bool bSuccess = false;
  59.  
  60.             try
  61.             {
  62.                 // Check for security
  63.                 if ((lngAmount > 500 || lngAmount < -500)
  64.                          && !ContextUtil.IsCallerInRole ("Managers"))
  65.                     throw new Exception ("Need 'Managers' role for amounts over $500");
  66.  
  67.                 result = truePost (lngAccountNo, lngAmount);
  68.  
  69.                 bSuccess = true;
  70.             }
  71.  
  72.             // Upon exit, always call SetComplete if happy, or SetAbort if unhappy.
  73.             // We do this because we never save state across method calls.
  74.             finally
  75.             {
  76.                     if (bSuccess)
  77.                         ContextUtil.SetComplete();
  78.                     else
  79.                         ContextUtil.SetAbort();
  80.             }
  81.  
  82.             return result;
  83.         }
  84.  
  85.         // F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++
  86.         //
  87.         // Function: TruePost
  88.         //
  89.         // TruePost() is the function that performs the actual work for the Account class.
  90.         // If an error occurs during execution, it will throw a ComFailException for Post() to
  91.         // handle.
  92.         //
  93.         // Args:     lngAccountNo -    Account to be posted
  94.         //           lngAmount -     Amount to be posted
  95.         // Returns:  String -        Account Balance
  96.         //
  97.         // F-F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---
  98.  
  99.         private String truePost (int lngAccountNo, int lngAmount) {
  100.  
  101.             Recordset adoRsBalance  = null;
  102.             Connection adoConn = null;
  103.             Field pField = null;
  104.             Fields pFields = null;
  105.             String result;
  106.  
  107.             try
  108.             {
  109.                 // Create ADOConnection object and initialize the connection
  110.  
  111.                 adoConn = (Connection) new Connection();
  112.                 String vDSN = "FILEDSN=BankSample";
  113.  
  114.                 adoConn.Open (vDSN, null, null, (int)CommandTypeEnum.adCmdUnspecified);
  115.  
  116.                 // Obtain the desired recordset with an SQL query
  117.                 Object  vRowCount = new Object();
  118.                 String strSQL = "UPDATE Account SET Balance = Balance + " + lngAmount +
  119.                                 " WHERE AccountNo = " + lngAccountNo;
  120.  
  121.         TryAgain:
  122.                 try
  123.                 {
  124.                     adoConn.Execute (strSQL, out vRowCount, (int)ExecuteOptionEnum.adExecuteNoRecords);
  125.                 }
  126.                 catch(Exception)
  127.                 {
  128.  
  129.                     ICreateTable ct = (ICreateTable)new CreateTable();
  130.                     ct.CreateAccount();
  131.                     goto TryAgain;
  132.                 }
  133.  
  134.                 // See whether the record is present.
  135.                 if ((int)vRowCount == 0)
  136.                     throw new Exception ("Error. Account " + lngAccountNo + " not on file.");
  137.  
  138.  
  139.                 strSQL = "SELECT Balance FROM Account WHERE AccountNo = " + lngAccountNo;
  140.                 adoRsBalance = adoConn.Execute (strSQL, out vRowCount, - 1);
  141.  
  142.                 // Get the appropriate fields
  143.                 pFields = adoRsBalance.Fields;
  144.  
  145.                 pField = pFields["Balance"];
  146.                 int lngBalance  = (int)pField.Value;
  147.  
  148.                 // Check if account is overdrawn, then prepare return string
  149.                 if ((lngBalance) < 0)
  150.                     throw new Exception("Error. Account " + lngAccountNo + " would be overdrawn by "
  151.                                         + (-lngBalance) + ". Balance is still " + (lngBalance - lngAmount) + ".");
  152.  
  153.                 if (lngAmount > 0)
  154.                     result = "Credit to";
  155.                 else
  156.                     result = "Debit from";
  157.                 result += " account " + lngAccountNo + ", balance is $" + lngBalance + ". (C#)";
  158.  
  159.                 return result;
  160.             }
  161.  
  162.             finally
  163.             {
  164.                 // cleanup that needs to occur whether we leave via a return or an exception.
  165.                 if (adoRsBalance != null)
  166.                 {
  167.                     if (adoRsBalance.State == (int)ObjectStateEnum.adStateOpen)
  168.                         adoRsBalance.Close();
  169.  
  170.                     Marshal.ReleaseComObject(adoRsBalance);
  171.                 }
  172.  
  173.                 if (adoConn != null)
  174.                 {
  175.                     if (adoConn.State == (int)ObjectStateEnum.adStateOpen)
  176.                         adoConn.Close();
  177.  
  178.                     Marshal.ReleaseComObject(adoConn);
  179.                 }
  180.             }
  181.         }
  182.     }
  183.  
  184. }