Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Source Property Example (VC++)
Collapse the table of content
Expand the table of content

Source Property Example (VC++)


This example demonstrates the Source property by opening three Recordset objects based on different data sources.

// Source_Property_Sample.cpp
// compile with: /EHsc
#import "msado15.dll" no_namespace rename("EOF", "EndOfFile")

#include <ole2.h>
#include <stdio.h>
#include <conio.h>

// Function declarations
inline void TESTHR(HRESULT x) { if FAILED(x) _com_issue_error(x); };
void SourceX();
void PrintProviderError(_ConnectionPtr pConnection);
void PrintComError(_com_error &e);

int main() {
   if ( FAILED(::CoInitialize(NULL)) )
      return -1;


void SourceX() {
   // Define string variables.
   _bstr_t strCmdSQL("Select title ,type, pubdate FROM titles ORDER BY title");  
   _bstr_t strSQL("SELECT title_ID AS TitleID, title AS Title, " 
      "publishers.pub_id AS PubID, pub_name AS PubName "
      "FROM publishers INNER JOIN titles " 
      "ON publishers.pub_id = titles.pub_id " 
      "ORDER BY Title");
   _bstr_t strCnn("Provider='sqloledb'; Data Source='My_Data_Source'; Initial Catalog='pubs'; Integrated Security='SSPI';");

   // Define ADO object pointers.  Initialize pointers on define.
   // These are in the ADODB:: namespace.
   _ConnectionPtr pConnection = NULL;
   _RecordsetPtr pRstTitles = NULL;
   _RecordsetPtr pRstPublishers = NULL;
   _RecordsetPtr pRstPublishersDirect = NULL;
   _RecordsetPtr pRstTitlesPublishers = NULL;
   _CommandPtr pCmdSQL = NULL;

   try {
      // Open a connection.
      pConnection->Open (strCnn, "", "", adConnectUnspecified);

      // Open a recordset based on a command object.
      pCmdSQL->ActiveConnection = pConnection;
      pCmdSQL->CommandText = strCmdSQL;
      pRstTitles = pCmdSQL->Execute(NULL, NULL, adCmdText);

      // Open a recordset based on a a table
      pRstPublishers->Open ("publishers", _variant_t((IDispatch *) pConnection, true),
         adOpenForwardOnly, adLockReadOnly, adCmdTable);

      // Open a recordset based on a table
      pRstPublishersDirect->Open ("publishers", _variant_t((IDispatch *) pConnection, true),
         adOpenForwardOnly, adLockReadOnly, adCmdTableDirect);

      // Open a recordset based on a SQL string.
      pRstTitlesPublishers->Open(strSQL, _variant_t((IDispatch *) pConnection, true),
         adOpenForwardOnly, adLockReadOnly, adCmdText);

      // Use the Source property to display the source of each recordset.
      printf("rstTitles source: \n%s\n\n",
         (LPCSTR)(_bstr_t) pRstTitles->GetSource().bstrVal);

      printf("rstPublishers source: \n%s\n\n",
         (LPCSTR)(_bstr_t) pRstPublishers->GetSource().bstrVal);

      printf("rstPublishersDirect source: \n%s\n\n",
         (LPCSTR)(_bstr_t) pRstPublishersDirect->GetSource().bstrVal);

      printf("rstTitlesPublishers source: \n%s\n\n",
         (LPCSTR)(_bstr_t) pRstTitlesPublishers->GetSource().bstrVal);
   catch (_com_error &e) {
      // Notify the user of errors if any.

   if (pRstTitles)
      if (pRstTitles->State == adStateOpen)
   if (pRstPublishers)
      if (pRstPublishers->State == adStateOpen)
   if (pRstPublishersDirect)
      if (pRstPublishersDirect->State == adStateOpen)
   if (pRstTitlesPublishers)
      if (pRstTitlesPublishers->State == adStateOpen)
   if (pConnection)
      if (pConnection->State == adStateOpen)

void PrintProviderError(_ConnectionPtr pConnection) {
   // Print Provider Errors from Connection object.
   // pErr is a record object in the Connection's Error collection.
   ErrorPtr pErr = NULL;

   if ( (pConnection->Errors->Count) > 0 ) {
      long nCount = pConnection->Errors->Count;
      // Collection ranges from 0 to nCount -1.
      for ( long i = 0 ; i < nCount ; i++ ) {
         pErr = pConnection->Errors->GetItem(i);
         printf("Error number: %x\t%s\n", pErr->Number, (LPCSTR) pErr->Description);

void PrintComError(_com_error &e) {
   _bstr_t bstrSource(e.Source());
   _bstr_t bstrDescription(e.Description());

   // Print Com errors.  
   printf("\tCode = %08lx\n", e.Error());
   printf("\tCode meaning = %s\n", e.ErrorMessage());
   printf("\tSource = %s\n", (LPCSTR) bstrSource);
   printf("\tDescription = %s\n", (LPCSTR) bstrDescription);
© 2015 Microsoft