home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / com / adosamp / runado / runado.cpp next >
Encoding:
C/C++ Source or Header  |  1998-04-03  |  6.7 KB  |  171 lines

  1. // Copyright (C) 1992-1998 Microsoft Corporation
  2. // All rights reserved.
  3. //
  4. // This source code is only intended as a supplement to the
  5. // Microsoft Visual C++ Language  Reference and related
  6. // electronic documentation provided with Microsoft Visual C++.
  7. // See these sources for detailed information regarding the
  8. // Microsoft Visual C++ product.
  9.  
  10. #import "..\server\adosamp.tlb" no_namespace rename("EOF", "ADOEOF")
  11. #include <stdio.h>
  12. #include <tchar.h>
  13.  
  14. //#define SQLSERVER
  15.  
  16. void dump_com_error(_com_error &e)
  17. {
  18.     _tprintf(_T("Oops - hit an error!\n"));
  19.     _tprintf(_T("\a\tCode = %08lx\n"), e.Error());
  20.     _tprintf(_T("\a\tCode meaning = %s\n"), e.ErrorMessage());
  21.     _bstr_t bstrSource(e.Source());
  22.     _bstr_t bstrDescription(e.Description());
  23.     _tprintf(_T("\a\tSource = %s\n"), (LPCTSTR) bstrSource);
  24.     _tprintf(_T("\a\tDescription = %s\n"), (LPCTSTR) bstrDescription);
  25. }
  26.  
  27. struct InitOle {
  28.     InitOle() { CoInitialize(NULL); }
  29.     ~InitOle() { CoUninitialize(); }
  30. } _init_InitOle_ ;
  31.  
  32. void main()
  33. {
  34.     try {
  35.     IADOTierPtr p(__uuidof(CADOTier));
  36.     p->Open("HotBikes", "sa", "");
  37.  
  38.     try {
  39.     p->ExecuteConnection("DROP TABLE HotBikes", VARIANT_FALSE);
  40.     } catch (_com_error& e) {
  41.     printf("TABLE does not exist -- ok, go create it...\n");
  42.     dump_com_error(e);
  43.     }
  44.  
  45.     printf("\nCreate table with 3 fields: an ID, model, and manufacturer.\n");
  46.     p->ExecuteConnection("CREATE TABLE HotBikes (ID INTEGER, Model VarChar(25), Manufacturer VarChar(80))", VARIANT_FALSE);
  47.  
  48.     printf("\nShove some data in it.\n");
  49.     p->AppendParameter(adInteger, 20L, adParamInput, -1);
  50.     p->AppendParameter(adVarChar, "ZX-11", adParamInput, 25);
  51.     p->AppendParameter(adVarChar, "Kawasaki", adParamInput, 80);
  52.     p->CommandText = "insert into HotBikes (ID, Model, Manufacturer) values (?, ?, ?)";
  53.     p->ExecuteCommand(VARIANT_FALSE, VARIANT_FALSE);
  54.  
  55.     _bstr_t query = "insert into HotBikes (ID, Model, Manufacturer) values (?, ?, ?)";
  56.     p->ParamQuery(query, 21, "Trail Breaker", "Rokon");
  57.     p->ParamQuery(query, 33, "900 Trident", "Triumph");
  58.  
  59.     p->OpenRecordset("SELECT * FROM HotBikes ORDER BY ID");
  60.     if (!p->Empty)
  61.     {
  62.         p->First();
  63.         while (!p->ADOEOF)
  64.         {
  65.             printf("%d. %S, %S\n", (long) p->Field[0L], (BSTR) (_bstr_t) p->Field[1L], (BSTR) (_bstr_t) p->Field[2L]);
  66.             p->Next();
  67.         }
  68.         p->First();
  69.         printf ("Now empty out the table.\n");
  70.         while (!p->ADOEOF)
  71.         {
  72.             p->Delete();
  73.             p->Next();
  74.         }
  75.     }
  76.  
  77.     printf("\nToo slow, and I feel the need for speed.  Put some bikes back in the table\n");
  78.     p->Requery();
  79.     for (!p->Empty && p->First(); !p->ADOEOF; p->Next())
  80.         printf("%d. %S, %S\n", (long) p->Field[0L], (BSTR) (_bstr_t) p->Field[1L], (BSTR) (_bstr_t) p->Field[2L]);
  81.  
  82.     p->ParamQuery(query, 1, "ZX-11", "Kawasaki");
  83.     p->ParamQuery(query, 2, "CBR-900RR", "Honda");
  84.     p->ParamQuery(query, 9, "GSX-R 1000", "Suzuki");
  85.     p->ParamQuery(query, 4, "ZX6.5", "Kawasaki");
  86.     p->ParamQuery(query, 5, "900SS", "Ducati");
  87.     p->ParamQuery(query, 6, "YB9SR", "Bimota");
  88.     p->ParamQuery(query, 7, "1400RR", "Buell");
  89.     p->ParamQuery(query, 8, "FZR 1000", "Yamaha");
  90.     p->ParamQuery(query, 3, "Trail 90", "Honda");
  91.     p->ParamQuery(query, 10, "R1100R", "BMW");
  92.     p->ParamQuery(query, 11, "Daytona", "Triumph");
  93.  
  94.     p->Requery();
  95.     for (!p->Empty && p->First(); !p->ADOEOF; p->Next())
  96.         printf("%d, %S, %S\n", (long) p->Field[0L], (BSTR) (_bstr_t) p->Field[1L], (BSTR) (_bstr_t) p->Field[2L]);
  97.  
  98.     p->ExecuteConnection("UPDATE HotBikes SET Model='ZX7' WHERE ID=4", VARIANT_FALSE);
  99.  
  100.     printf("\nFix Typo...ZX7, not ZX6.5\n");
  101.     p->Requery();
  102.     for (!p->Empty && p->First(); !p->ADOEOF; p->Next())
  103.         printf("%d. %S, %S\n", (long) p->Field[0L], (BSTR) (_bstr_t) p->Field[1L], (BSTR) (_bstr_t) p->Field[2L]);
  104.     printf("\nThat's better -- hey, how did the Trail 90 make it on this list???\n");
  105.  
  106.     p->CloseRecordset();
  107.     printf("\nAdd a new field for top speed, enter relevant data and order by TopSpeed.\n");
  108.     p->ExecuteConnection("ALTER TABLE HotBikes ADD TopSpeed INTEGER", VARIANT_FALSE);
  109.  
  110.     p->ExecuteConnection("UPDATE HotBikes SET TopSpeed=170 WHERE ID=1", VARIANT_FALSE);
  111.     p->ExecuteConnection("UPDATE HotBikes SET TopSpeed=163 WHERE ID=2", VARIANT_FALSE);
  112.     p->ExecuteConnection("UPDATE HotBikes SET TopSpeed=0 WHERE ID=3", VARIANT_FALSE);
  113.     p->ExecuteConnection("UPDATE HotBikes SET TopSpeed=155 WHERE ID=4", VARIANT_FALSE);
  114.     p->ExecuteConnection("UPDATE HotBikes SET TopSpeed=137 WHERE ID=5", VARIANT_FALSE);
  115.     p->ExecuteConnection("UPDATE HotBikes SET TopSpeed=132 WHERE ID=6", VARIANT_FALSE);
  116.     p->ExecuteConnection("UPDATE HotBikes SET TopSpeed=130 WHERE ID=7", VARIANT_FALSE);
  117.     p->ExecuteConnection("UPDATE HotBikes SET TopSpeed=164  WHERE ID=8", VARIANT_FALSE);
  118.     p->ExecuteConnection("UPDATE HotBikes SET TopSpeed=165 WHERE ID=9", VARIANT_FALSE);
  119.     p->ExecuteConnection("UPDATE HotBikes SET TopSpeed=125 WHERE ID=10", VARIANT_FALSE);
  120.     p->ExecuteConnection("UPDATE HotBikes SET TopSpeed=150 WHERE ID=11", VARIANT_FALSE);
  121.  
  122.     p->OpenRecordset("SELECT * FROM HotBikes ORDER BY TopSpeed");
  123.     for (!p->Empty && p->First(); !p->ADOEOF; p->Next())
  124.         printf("ID=%d, TopSpeed=%d, %S, %S\n", (long) p->Field[0L], (long) p->Field[3L], (BSTR) (_bstr_t) p->Field[1L], (BSTR) (_bstr_t) p->Field[2L]);
  125.  
  126.     p->ExecuteConnection("DELETE FROM HotBikes WHERE TopSpeed=0", VARIANT_FALSE);
  127.  
  128.     printf("\nDelete all entries with NO TopSpeed.\n");
  129.     p->Requery();
  130.     for (!p->Empty && p->First(); !p->ADOEOF; p->Next())
  131.         printf("ID=%d, TopSpeed=%d, %S, %S\n", (long) p->Field[0L], (long) p->Field[3L], (BSTR) (_bstr_t) p->Field[1L], (BSTR) (_bstr_t) p->Field[2L]);
  132.  
  133.     p->CloseRecordset();
  134. #ifndef SQLSERVER
  135.     // NOTE:  SQLServer does not allow this command
  136.     p->ExecuteConnection("ALTER TABLE HotBikes DROP COLUMN ID", VARIANT_FALSE);
  137.  
  138.     printf("\nDrop the ID field\n");
  139.     p->OpenRecordset("SELECT * FROM HotBikes ORDER BY TopSpeed");
  140.     for (!p->Empty && p->First(); !p->ADOEOF; p->Next())
  141.         printf("TopSpeed=%d, %S, %S\n", (long) p->Field[2L], (BSTR) (_bstr_t) p->Field[0L], (BSTR) (_bstr_t) p->Field[1L]);
  142.  
  143.     p->CloseRecordset();
  144. #endif
  145.     printf("\nDelete all entries with insufficient top speed  (Okay, less than 160).\n");
  146.     p->OpenRecordset("SELECT * FROM HotBikes WHERE TopSpeed<160");
  147.     p->First();
  148.     while (!p->ADOEOF)
  149.     {
  150.         p->Delete();
  151.         p->Next();
  152.     }
  153.  
  154.     p->CloseRecordset();
  155.     p->OpenRecordset("SELECT * FROM HotBikes ORDER BY TopSpeed DESC");
  156.     for (!p->Empty && p->First(); !p->ADOEOF; p->Next())
  157. #ifdef SQLSERVER
  158.         printf("ID=%d, TopSpeed=%d, %S, %S\n", (long) p->Field[0L], (long) p->Field[3L], (BSTR) (_bstr_t) p->Field[1L], (BSTR) (_bstr_t) p->Field[2L]);
  159. #else
  160.         printf("TopSpeed=%d, %S, %S\n", (long) p->Field[2L], (BSTR) (_bstr_t) p->Field[0L], (BSTR) (_bstr_t) p->Field[1L]);
  161. #endif
  162.  
  163.     p->CloseRecordset();
  164.     p->Close();
  165.     p->ADORelease();
  166.  
  167.     } catch (_com_error& e) {
  168.     dump_com_error(e);
  169.     }
  170. }
  171.