home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / dotNETSDK / SETUP.EXE / netfxsd1.cab / FL_dbtransactionswithacommand_cpp________.3643236F_FC70_11D3_A536_0090278A1BB8 < prev    next >
Encoding:
Text File  |  2002-06-20  |  2.4 KB  |  71 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.  
  20. using namespace System;
  21. using namespace System::Data;
  22. using namespace System::Data::SqlClient;
  23.  
  24. __gc class dbtransactionswithacommand
  25. {
  26. public:
  27.  
  28.   void Run()
  29.   {
  30.     SqlConnection * myConnection = new SqlConnection(S"server=(local)\\NetSDK;Integrated Security=SSPI;database=northwind");
  31.     SqlCommand * mySqlCommand;
  32.     SqlTransaction * myTransaction;
  33.  
  34.     // Restore database to near it's original condifition so sample will work correctly.
  35.     myConnection->Open();
  36.     mySqlCommand = new SqlCommand(S"DELETE FROM Region WHERE (RegionID = 100) OR (RegionID = 101)", myConnection);
  37.     mySqlCommand->ExecuteNonQuery();
  38.  
  39.     myTransaction = myConnection->BeginTransaction();
  40.  
  41.     // Assign transaction object for a pending local transaction
  42.     mySqlCommand->Transaction = myTransaction;
  43.  
  44.     try
  45.     {
  46.       mySqlCommand->CommandText = S"Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
  47.       mySqlCommand->ExecuteNonQuery();
  48.       mySqlCommand->CommandText = S"Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
  49.       mySqlCommand->ExecuteNonQuery();
  50.       myTransaction->Commit();
  51.       Console::WriteLine(S"Both Records written to database");
  52.     }
  53.     catch(Exception * e)
  54.     {
  55.       myTransaction->Rollback();
  56.       Console::WriteLine(e->ToString());
  57.       Console::WriteLine(S"Neither record written to database.");
  58.     }
  59.     __finally
  60.     {
  61.       myConnection->Close();
  62.     }
  63.   }
  64. };
  65.  
  66. void main()
  67. {
  68.   dbtransactionswithacommand * mydbtransactionswithacommand = new dbtransactionswithacommand();
  69.   mydbtransactionswithacommand->Run();
  70. }
  71.