home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 32 / IOPROG_32.ISO / SOFT / SqlEval7 / devtools / samples / sqldmo / cpp / Smartptr / Smartptr.cpp next >
Encoding:
C/C++ Source or Header  |  1998-08-11  |  7.9 KB  |  291 lines

  1. // **********************************************************************
  2. // Smartptr -    SQL-DMO smartpointer sample application uses
  3. //                the built-in Visual C 5.0 _comp_ptr_t support. 
  4. //                If prefered, adapt to use CComPtr or CComQIPtr
  5. //                (VC 5.0 and later)
  6. //
  7. //                the _com_error calls ->Release automatically when it
  8. //                goes out of scope to clean itself up. 
  9. //                there is no need to call delete or release.
  10. // **********************************************************************
  11.  
  12. #define STRICT                    // Strict type checking.
  13. #define WIN32_LEAN_AND_MEAN        // Do not include the world.
  14. #define INC_OLE2                // Include OLE/COM files
  15.  
  16. #include <windows.h>
  17. #include <stdio.h>
  18. #include <comdef.h>             // Compiler support for _bstr_t
  19.  
  20. // The #import statement is locale specific. Change the path to import
  21. // from the locale specific .rll file installed.
  22. #import "c:\mssql7\binn\resources\1033\sqldmo.rll" no_namespace
  23.  
  24. // **********************************************************************
  25. // Function declarations.
  26. // **********************************************************************
  27. void    vDisplayError(_com_error & pCE);
  28. BOOL    bSetDefaults(_SQLServerPtr & spSQLServer);
  29. BOOL    bConnect(_SQLServerPtr & spSQLServer);
  30. void    vDisconnect(_SQLServerPtr & spSQLServer);
  31. void    vDoQuery(_SQLServerPtr & spSQLServer, LPCTSTR strQuery);
  32. void    vDisplayResults(QueryResultsPtr & spQueryResults);
  33.  
  34. // **********************************************************************
  35. // Macros.
  36. // **********************************************************************
  37. #define _MAX_COL        25
  38. #define _MAX_COL_FMT    "%-25s "
  39.  
  40. #define _SERVER            "."     // Local server
  41. #define _USER            "sa"
  42. #define _PWD            ""
  43. #define _QUERYSTMT        "SELECT LastName, FirstName, HireDate FROM " \
  44.                         " Northwind.dbo.Employees" 
  45.  
  46. // **********************************************************************
  47. // main()
  48. // **********************************************************************
  49. void main(void)
  50. {
  51.     printf("\n\nSQL-DMO Smart Pointer Sample\n");
  52.  
  53.  
  54.     HRESULT hr;
  55.     if SUCCEEDED(hr = CoInitialize(NULL) )
  56.     {
  57.         // Use the interface appropriate for LPSQLDMOSERVER.
  58.         try
  59.         {
  60.             _SQLServerPtr spSQLServer;
  61.  
  62.             if (SUCCEEDED(spSQLServer.CreateInstance(__uuidof(SQLServer))))
  63.             {
  64.                 try
  65.                 {
  66.                     if (TRUE == bSetDefaults(spSQLServer))
  67.                     {
  68.                         vDoQuery(spSQLServer, _QUERYSTMT);
  69.                     }
  70.  
  71.                     // Because the spSQLServer was created in main, it
  72.                     // will not go out of scope until the end of main.
  73.                     //
  74.                     // However, this is after the OleUninitilize is called
  75.                     // and causes an error.
  76.                     //
  77.                     // For this special case, call the .Release to
  78.                     // clean up the pointer and do the work.
  79.                     spSQLServer.Release();
  80.                 }
  81.                 catch(_com_error pCE)
  82.                 {
  83.                     vDisplayError(pCE);
  84.                     spSQLServer.Release();     //    Free the interface.
  85.                 }
  86.             }
  87.             else
  88.             {
  89.                 printf("\nUnable to create the SQLServer object.");
  90.             }
  91.         }
  92.         catch(_com_error pCE)
  93.         {
  94.             vDisplayError(pCE);
  95.         }
  96.  
  97.         CoUninitialize();
  98.     }
  99.     else
  100.     {
  101.         printf("\nCall to CoInitialize failed.");
  102.     }
  103.     
  104.     printf("\n");
  105.  
  106. }
  107.  
  108. // **********************************************************************
  109. // vDisplayError
  110. //
  111. // Display error information.
  112. // **********************************************************************
  113. void vDisplayError(_com_error & pCE)
  114. {
  115.     //    Assuming ANSI build at this time.
  116.     printf( "\n%s Error: %ld\r\n"
  117.             "%s\r\n"
  118.             "%s\r\n",
  119.             (char*)pCE.Source(),
  120.                    pCE.Error(),
  121.             (char*)pCE.Description(),
  122.             (char*)pCE.ErrorMessage());
  123. }
  124.  
  125. // **********************************************************************
  126. // bSetDefaults
  127. //
  128. // Accepts the current address so the RefCount does not need to be 
  129. // touched. There is no reason to increment it on entry and then 
  130. // decrement it on exit, just to be doing it.
  131. // **********************************************************************
  132. BOOL bSetDefaults(_SQLServerPtr & spSQLServer)
  133. {
  134.     BOOL bRC = FALSE;
  135.  
  136.     printf("\nSetting SQL Server object properties.");
  137.  
  138.     try
  139.     {
  140.         spSQLServer->PutLoginTimeout(10);
  141.         spSQLServer->PutApplicationName("SmartPointers");
  142.         spSQLServer->PutHostName("Test");
  143.         spSQLServer->PutNetPacketSize(1024);
  144.  
  145.         bRC = TRUE;
  146.     }
  147.     catch(_com_error pCE)
  148.     {
  149.         vDisplayError(pCE);
  150.     }
  151.     return bRC;
  152. }
  153.  
  154.  
  155. // **********************************************************************
  156. // bConnect
  157. //
  158. // Connect to the SQL Server.
  159. // **********************************************************************
  160. BOOL bConnect(_SQLServerPtr & spSQLServer)
  161. {
  162.     BOOL bRC = FALSE;
  163.  
  164.     printf("\nAttempting to connect to %s as %s", _SERVER, _USER);
  165.  
  166.     try
  167.     {
  168.         spSQLServer->Connect(_SERVER, _USER, _PWD);
  169.  
  170.         bRC = TRUE;
  171.     }
  172.     catch(_com_error pCE)
  173.     {
  174.         vDisplayError(pCE);
  175.     }
  176.     return bRC;
  177. }
  178.  
  179.  
  180. // **********************************************************************
  181. // vDisconnect
  182. //
  183. // Disconnect from the SQL Server.
  184. // **********************************************************************
  185. void vDisconnect(_SQLServerPtr & spSQLServer)
  186. {
  187.     printf("\nDisconnecting from %s", _SERVER);
  188.  
  189.     try
  190.     {
  191.         spSQLServer->Close();
  192.     }
  193.     catch(_com_error pCE)
  194.     {
  195.         vDisplayError(pCE);
  196.     }
  197. }
  198.  
  199. // **********************************************************************
  200. // vDoQuery
  201. //
  202. // Execute query capturing generated result sets in a QueryResults
  203. // object.
  204. // **********************************************************************
  205. void vDoQuery(_SQLServerPtr & spSQLServer, LPCTSTR strQuery)
  206. {
  207.     QueryResultsPtr spQueryResults;
  208.  
  209.     if (TRUE == bConnect(spSQLServer))
  210.     {
  211.         printf("\nExecuting %s\n", strQuery);
  212.  
  213.         try
  214.         {
  215.             spQueryResults = spSQLServer->ExecuteWithResults(strQuery);
  216.  
  217.             vDisplayResults(spQueryResults);
  218.         }
  219.         catch(_com_error pCE)
  220.         {
  221.             vDisplayError(pCE);
  222.         }
  223.  
  224.         vDisconnect(spSQLServer);
  225.  
  226.     }
  227. }
  228.  
  229. // **********************************************************************
  230. // vDisplayResults
  231. //
  232. // Print result set(s) information and contents.
  233. // **********************************************************************
  234. void vDisplayResults(QueryResultsPtr & spQueryResults)
  235. {
  236.     long    lMaxColLen = 0;
  237.     char    strDisplayData[_MAX_COL +1] = "";
  238.     _bstr_t bstrColName;
  239.     _bstr_t bstrRowInfo;
  240.  
  241.     try
  242.     {
  243.         // Loop through the results sets.
  244.         for (long lSet = 1; lSet <= spQueryResults->GetResultSets(); lSet++)
  245.         {
  246.             spQueryResults->PutCurrentResultSet(lSet);
  247.             printf("\n >>> Result set #%ld\n", lSet);
  248.  
  249.             // Print the column headers.
  250.             for (long lCols = 1; lCols <= spQueryResults->GetColumns(); lCols++)
  251.             {
  252.                 // You do not need to call SysFreeString when using the smart
  253.                 // BSTR pointer (_bstr_t). The copy operator of the _bstr_t
  254.                 // performs a free on any prior value and then handles the
  255.                 // new data, preventing a memory leak when used in this loop.
  256.                 //
  257.                 // For complete details, see the definition of the _bstr_t
  258.                 // copy coperator in COMUTIL.H.
  259.                 bstrColName = spQueryResults->GetColumnName(lCols);
  260.  
  261.                 // Assuming ANSI build for sample so casting as char*.
  262.                 strncpy(strDisplayData, (char *)bstrColName, _MAX_COL);
  263.                 printf(_MAX_COL_FMT, strDisplayData);
  264.             }
  265.             
  266.             printf("\n");
  267.  
  268.             // Display the data.
  269.             for (long lRows = 1; lRows <= spQueryResults->GetRows(); lRows++)
  270.             {
  271.                 for (long lCols = 1; lCols <= spQueryResults->GetColumns(); lCols++)
  272.                 {
  273.                     bstrRowInfo = spQueryResults->GetColumnString(lRows, lCols);
  274.  
  275.                     strncpy(strDisplayData, (char *)bstrRowInfo, _MAX_COL);
  276.                     printf(_MAX_COL_FMT, strDisplayData);
  277.                 }
  278.                 // End of row.
  279.                 printf("\n");
  280.             }
  281.  
  282.             printf("\nRows processed (%ld)", spQueryResults->GetRows());
  283.         
  284.         } // End of FOR loop processing a result set.
  285.     }
  286.     catch(_com_error pCE)
  287.     {
  288.         vDisplayError(pCE);
  289.     }
  290. }
  291.