Export (0) Print
Expand All
Expand Minimize

GetString Method Example (VC++)

This example demonstrates the GetString method.

Assume you are debugging a data access problem and want a quick, simple way of printing the current contents of a small Recordset.

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

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

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

inline char* mygets(char* strDest, int n) {   
   char strExBuff[10];
   char* pstrRet = fgets(strDest, n, stdin);

   if (pstrRet == NULL)
      return NULL;

   // Exhaust the input buffer.
   if (!strrchr(strDest, '\n'))
      do {
         fgets(strExBuff, sizeof(strExBuff), stdin);
      } while (!strrchr(strExBuff, '\n'));
      // Replace '\n' with '\0'
      strDest[strrchr(strDest, '\n') - strDest] = '\0';

   return pstrRet;

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

void GetStringX() {
   HRESULT  hr = S_OK;
  char * token1;

   // Define ADO object pointers.
   // Initialize pointers on define.
   // These are in the ADODB::  namespace.
   _ConnectionPtr pConnection = NULL;
   _RecordsetPtr pRstAuthors = NULL;

   // Define string variables.
   _bstr_t strCnn("Provider='sqloledb'; Data Source=My_Data_Source; Initial Catalog='pubs'; Integrated Security='SSPI';");
   _bstr_t varOutput; 
   char *strPrompt = "Enter a state (CA, IN, KS, MD, MI, OR, TN, UT): ";
   char strState[3], *pState;

   try {
      // Open connection.

      // Open a command object for a stored procedure, 
      // with one parameter.

      mygets(strState, 3);

      pState = strtok_s(strState," \t", &token1);

      char strQry[100];
      _snprintf_s(strQry, _countof(strQry), sizeof(strQry)-1, "SELECT au_fname, au_lname, address, city "
         "FROM authors WHERE state = '%s'", pState);
      strQry[sizeof(strQry)-1] = '\0';

      pConnection->Open (strCnn, "", "", adConnectUnspecified);

      pRstAuthors->Open(strQry, _variant_t((IDispatch*)pConnection, true), 
         adOpenStatic, adLockReadOnly, adCmdText);

      if (pRstAuthors->RecordCount > 0)  {
         // Use all defaults: get all rows, TAB column delimiter, 
         // CARRIAGE RETURN row delimiter, empty-string null delimiter
         long lRecCount =  pRstAuthors->RecordCount;
         _bstr_t varTab("\t");
         _bstr_t varRet("\r");
         _bstr_t varNull("");
         varOutput = pRstAuthors->GetString(adClipString, lRecCount, varTab, varRet, varNull);
         printf("State = '%s'\n", strState);
         printf ("Name            Address            City\n");
         printf ("%s\n", (LPCTSTR)varOutput);
         printf("\nNo rows found for state = '%s'\n", pState);
   catch(_com_error &e) {
      // Notify the user of errors if any.
      // Pass a connection pointer accessed from the Recordset.

   // Clean up objects before exit.
   if (pRstAuthors)
      if (pRstAuthors->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("\t Error number: %x\t%s", pErr->Number, 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);

Enter a state (CA, IN, KS, MD, MI, OR, TN, UT): State = 'md'
Name            Address            City
Sylvia   Panteley   1956 Arlington Pl.   Rockville
© 2015 Microsoft