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

  1. //-----------------------------------------------------------------------
  2. //  This file is part of the Microsoft .NET SDK Code Samples.
  3. // 
  4. //  Copyright (C) Microsoft Corporation.  All rights reserved.
  5. // 
  6. //This source code is intended only as a supplement to Microsoft
  7. //Development Tools and/or on-line documentation.  See these other
  8. //materials for detailed information regarding Microsoft code samples.
  9. // 
  10. //THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
  11. //KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  12. //IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  13. //PARTICULAR PURPOSE.
  14. //-----------------------------------------------------------------------
  15.  
  16. #using <mscorlib.dll>
  17. #using <System.dll>
  18. #using <System.Data.dll>
  19. #using <System.EnterpriseServices.dll>
  20.  
  21.  
  22. using namespace System;
  23. using namespace System::Data;
  24. using namespace System::Reflection;
  25. using namespace System::Data::SqlClient;
  26. using namespace System::EnterpriseServices;
  27.  
  28. [assembly: AssemblyKeyFileAttribute("keyfile.snk")];
  29. [TransactionAttribute(TransactionOption::Required)]
  30. public __gc class myServicedComponent : public ServicedComponent
  31. {
  32. private:
  33.     SqlConnection * m_rConnection;
  34.  
  35. public:
  36.     myServicedComponent()
  37.     {
  38.     }
  39.  
  40.   __property SqlConnection * get_SqlConn() { return m_rConnection; }
  41.   
  42.  
  43.   __property ConnectionState * get_State()  { return __box (m_rConnection->State); }
  44.  
  45.  
  46.   void myServicedComponentConnect(String * ConnectionString)
  47.   {
  48.     m_rConnection = new SqlConnection(ConnectionString);
  49.     m_rConnection->Open();
  50.   }
  51.  
  52.   void myServicedComponentClose()
  53.   {
  54.       if (m_rConnection != 0)
  55.         m_rConnection->Close();
  56.   }
  57.  
  58.   void myServicedComponentDispose()
  59.   {
  60.       if (m_rConnection != 0)
  61.           m_rConnection->Dispose();
  62.   }
  63.  
  64.   void myServicedComponentChangeDatabase(String * DBName)
  65.   {
  66.       if (m_rConnection != 0)
  67.           m_rConnection->ChangeDatabase(DBName);
  68.   }
  69.  
  70.   SqlCommand * myServicedComponentCreateCommand()
  71.   {
  72.     return new SqlCommand(0, m_rConnection);
  73.   }
  74.  
  75.   void myServicedComponentSetComplete()  // to commit changes
  76.   {
  77.     Console::WriteLine("Committing connection\'s transaction.");
  78.     ContextUtil::SetComplete();
  79.     Console::WriteLine("SetComplete");
  80.   }
  81.  
  82.   void myServicedComponentSetAbort()     // to rollback the distributed transaction
  83.   {
  84.     Console::WriteLine("Aborting connection\'s transaction.");
  85.     ContextUtil::SetAbort();
  86.     Console::WriteLine("SetAbort");
  87.   }
  88. };
  89.  
  90. __gc class distribtransaction
  91. {
  92. public:
  93.  
  94.   void Run()
  95.   {
  96.     SqlCommand * mySqlCommand;
  97.     myServicedComponent * mySC = new myServicedComponent();
  98.  
  99.     mySC->myServicedComponentConnect(S"server=(local)\\NetSDK;Integrated Security=SSPI;database=northwind");
  100.     Console::Write("connection state is: ");
  101.     Console::WriteLine(mySC->State);
  102.  
  103.     mySqlCommand = new SqlCommand(0, mySC->SqlConn);
  104.  
  105.     try
  106.     {
  107.       // Clean up the database to restore to its original state.
  108.       mySqlCommand->CommandText = "if exists (select * from sysobjects where name = 'TestTable' and type = 'U') drop table TestTable";
  109.       mySqlCommand->ExecuteNonQuery();
  110.  
  111.       // Create table "TestTable".
  112.       Console::WriteLine("Creating table TestTable.");
  113.       mySqlCommand->CommandText = "create table TestTable (c1 int identity, c2 int)";
  114.       mySqlCommand->ExecuteNonQuery();
  115.  
  116.       // Create unique index ss on TestTable.
  117.       Console::WriteLine("Creating unique index ss on TestTable.");
  118.       mySqlCommand->CommandText = "create unique index ss on TestTable(c1)";
  119.       mySqlCommand->ExecuteNonQuery();
  120.  
  121.       // Insert into TestTable values.
  122.       Console::WriteLine("Inserting into TestTable values.");
  123.       mySqlCommand->CommandText = "insert into TestTable values (11)";
  124.       mySqlCommand->ExecuteNonQuery();
  125.  
  126.       // Commit the transaction.
  127.       mySC->myServicedComponentSetComplete();
  128.     }
  129.     catch (Exception * e)
  130.     {
  131.       Console::Write(e->ToString());
  132.  
  133.       // Rollback the transaction.
  134.       mySC->myServicedComponentSetAbort();
  135.     }
  136.     __finally
  137.     {
  138.       //mySC->myServicedComponentClose();
  139.       //Console::WriteLine("connection state is: " + mySC->State);
  140.     }
  141.   }
  142. };
  143.  
  144. void main()
  145. {
  146.   distribtransaction * mydistribtransaction = new distribtransaction();
  147.   mydistribtransaction->Run();
  148. }
  149.  
  150.  
  151.