SOL9 Sample: ADORecordsetApplet

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * ADORecordsetApplet.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL9
// 2009/05/20

// Assumes that environment of SQL Server 2008 and SQLClient1.0 
// Please create a database 'Sample' for this example
// and table 'Person' 

#include <sol/sql/ADOApplet.h>
#include <sol/Locale.h>

namespace SOL {

class ADORecordsetApplet: public ADOApplet {

public:
  /**
   * Constructor
   */
  ADORecordsetApplet(int argc, const TCHAR** argv)
    :ADOApplet(argc, argv)
  {
  }

public:
  ~ADORecordsetApplet()
  {
  }

public:
  /**
   * ADORecordsetApplet main procedure
   */
  virtual void run()
  {
    printf("1 Start\n");
    ADOConnection connection = getConnection();

    ADORecordset recordset;
    recordset.createInstance();

    _bstr_t query("SELECT * FROM Person");    
    printf("2 Try to open a recordset for source = \"%s\"\n", (const char*)query);

    recordset.open(
      query,
      connection.getConnectionPtr(),
      ADODB::adOpenStatic,
      ADODB::adLockReadOnly);
    printf("3 OK opened a recordset\n");
    
    _bstr_t criteria = "Email LIKE 'foo@%'";
    printf("4 Try to recordset.find() method with a criteria \"%S\"", (const wchar_t*)criteria);
    recordset.find(criteria, 0, ADODB::adSearchForward);
  
    printf("5 Found a record for a criteria = \"%S\"\n", (const wchar_t*)criteria);
  
    // Persist the recordset to an xml file by calling save method with a file name
    // and ADODB::adPersistXML
    const TCHAR* fileName = _T("person.xml");

    if (GetFileAttributes(fileName) != 0xffffffff) {
      DeleteFile(fileName);
      _tprintf(_T("6 Deleted an existing xml file:%s\n"), fileName);
    }
    
    recordset.save(fileName, ADODB::adPersistXML);
    printf("7 OK, saved the recordset to the fileName:%s\n", (const char*)fileName);
  
    // Persist the recordset to a ADOStream  by calling save method with a ADO stream
    // and ADODB::adPersistXML
    ADOStream stream;
    stream.createInstance();
    printf("8 OK, created a stream\n");

    stream.open();
    printf("9 OK, stream.open()\n");
    _variant_t destination((IDispatch*)stream.getStreamPtr());

    printf("10 Try to save a recordset to a stream\n");

    recordset.save(destination, ADODB::adPersistXML);
    printf("11 OK, saved the recordset to a stream\n");
    //Close already opened recordset.      
    recordset.close();

    //OK create an instance of ADORecordset.
    ADORecordset recset2;
    recset2.createInstance();

    printf("12 Try to open a recordset from a persitent XML file %s\n", fileName);
    recset2.open(fileName,
        _variant_t("Provider=MSPersist;"),
        ADODB::adOpenForwardOnly,
        ADODB::adLockReadOnly,
        ADODB::adCmdFile);
    printf("13 OK, opened recordset from the file %s\n", fileName);
    recset2.close();

    //OK create an instance of ADORecordset.
    ADORecordset recset3;
    recset3.createInstance();

    stream.putPosition(0L);

    printf("14 Try to open a recordset from a stream of destionation\n");
    recset3.open(destination,  //ADOStream
        vtMissing,  //Unspecified
        ADODB::adOpenForwardOnly,
        ADODB::adLockReadOnly,
        ADODB::adCmdFile);
    printf("15 OK, opened recordset from a stream\n");
    printf("16 Try to call recordset.getString() to get all rows\n");
    _bstr_t string = recset3.getString();
        
    printf("17 OK, recorset string:\n%S\n", (const wchar_t*)string);
    recset3.close();
  }
};

}


// Console application starts here.
void _tmain(int argc, const TCHAR** argv)
{
  Locale locale;
  try {

    ADORecordsetApplet recordsetApplet(argc, argv);
  
    recordsetApplet.start();

  } catch(Exception& ex) {
    ex.dump();
  } catch(...){
    printf("Exception:Unknown\n");
  }
}


Last modified: 2 May 2016

Copyright (c) 2016 Antillia.com ALL RIGHTS RESERVED.