home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / dotNETSDK / SETUP.EXE / netfxsd1.cab / FL_xmlfromsqlsrv_cpp________.3643236F_FC70_11D3_A536_0090278A1BB8 < prev    next >
Encoding:
Text File  |  2002-06-20  |  2.4 KB  |  75 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.Xml.dll>
  19. #using <System.Data.dll>
  20.  
  21. using namespace System;
  22. using namespace System::Data;
  23. using namespace System::Xml;
  24. using namespace System::Data::SqlClient;
  25.  
  26. __gc class xmlfromsqlsrv
  27. {
  28. public:
  29.  
  30.   void Run()
  31.   {
  32.     String * sConnection = S"server=(local)\\NetSDK;Integrated Security=SSPI;database=northwind";
  33.     SqlConnection * mySqlConnection = new SqlConnection(sConnection);
  34.     SqlCommand * mySqlCommand = new SqlCommand("select * from customers FOR XML AUTO, XMLDATA", mySqlConnection);
  35.     mySqlCommand->CommandTimeout = 15;
  36.  
  37.     try
  38.     {
  39.       mySqlConnection->Open();
  40.  
  41.       // Now create the DataSet and fill it with xml data.
  42.       DataSet * myDataSet1 = new DataSet();
  43.       myDataSet1->ReadXml(__try_cast<XmlTextReader*>(mySqlCommand->ExecuteXmlReader()), XmlReadMode::Fragment);
  44.  
  45.       // Modify to match the other dataset
  46.       myDataSet1->DataSetName = "NewDataSet";
  47.  
  48.  
  49.       // Get the same data through the provider.
  50.       SqlDataAdapter * mySqlDataAdapter = new SqlDataAdapter("select * from customers", sConnection);
  51.       DataSet * myDataSet2 = new DataSet();
  52.       mySqlDataAdapter->Fill(myDataSet2);
  53.  
  54.       // Write data to files: data1.xml and data2.xml for comparison.
  55.       myDataSet1->WriteXml("data1.xml");
  56.       myDataSet2->WriteXml("data2.xml");
  57.       Console::WriteLine ("Data has been writen to the output files: data1.xml and data2.xml");
  58.     }
  59.     catch(Exception * e)
  60.     {
  61.       Console::WriteLine(e->ToString());
  62.     }
  63.     __finally
  64.     {
  65.       mySqlConnection->Close();
  66.     }
  67.   }
  68. };
  69.  
  70. void main()
  71. {
  72.     xmlfromsqlsrv * myxmlfromsqlsrv = new xmlfromsqlsrv();
  73.     myxmlfromsqlsrv->Run();
  74. }
  75.