Code Samples -- Visual C++

Example #1 - Retrieve Data
#include <tchar.h>
#import "OX2.dll"
#include <iostream.h>

#define CMDUNKNOWN  0 // detect command type automatically
#define CMDSQLSTMT  1 // SQL statement
#define CMDSTOREDPROC 2 // Stored procedure or a function

int main(int argc, char* argv[])
{
 CoInitialize(NULL);

 OX2Lib::IConnectionPtr conn;
 if(S_OK == conn.CreateInstance("OpenX2.Connection")) {
  if(VARIANT_TRUE == conn->Connect(_bstr_t("ms_ox1"))) {
   OX2Lib::ICommandOXPtr cmd = conn->CreateCommand(_bstr_t("SELECT * FROM authors"), VARIANT_TRUE, CMDSQLSTMT);
   if(cmd != NULL) {
    while(cmd->MoveNext() == VARIANT_TRUE) {
     for(int i = 1; i <= cmd->FieldCount; i++) {
      _variant_t vIndex((long)i);
      _bstr_t str = cmd->FieldValueAsString[vIndex];
      cout << (LPCTSTR)str << _T("\t");
     }
     cout << endl;
    }
    cmd.Release();
   }
  }
  conn.Release();
 }

 CoUninitialize();
 return 0;
}


© 2001 www.openx.ca