SOL9 Sample: RecordsetFieldsApplet

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


// SOL9
// 2009/05/27
// Assumes that environment of SQL Server 2008 and SQLClient1.0 
// Please create a database 'Sample' for this example
// and following table 'SolUser'.
/*
Create Table SolUser(
  ID int not NULL primary key,
  Name  varchar(255) not NULL,
  Sex   varchar(10),
  Age   int,
  Birthday  Date,
  Nationality varchar(255),
  Email varchar(255),
  Telephone varchar(128),
  Address varchar(255),
  Company varchar(255));
*/

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

namespace SOL {

class RecordsetFieldsApplet: public ADOApplet {

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

public:
  ~RecordsetFieldsApplet()
  {
  }

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

    try {
      _bstr_t useDatabase = "Use Sample";

      ADORecordset recordset;
      long recordsAffected = connection.execute(useDatabase, recordset);
      printf("OK, connection.execute(\"%S\") recordsAffected(%ld)\n",
        (const wchar_t*)useDatabase, recordsAffected);

    } catch (_com_error& e) {
      COMError error(e);
      error.dump();
    }
    _bstr_t query("SELECT ID, Name, Age, Company FROM SolUser WHERE Age>25 AND Age<28 ORDER by Age");

    ADORecordset recordset;
    recordset.createInstance();

    recordset.open(query, connection.getConnectionPtr(), 
      ADODB::adOpenKeyset,
      ADODB::adLockOptimistic);

    printf("2 OK, recordset.open(\"%S\")\n", (const wchar_t*)query);
    printf("3 pageSize=%ld\n", recordset.getPageSize());
    printf("4 Sort=%S\n", (const wchar_t*)recordset.getSort());

    PerformanceCounter counter;
    counter.start();
    printf("5 Start: recordset walk\n");
  
    while(!recordset.getadoEOF()){
      //Get an object of ADOFields from the current recordset object.
      ADOFields colFields;
      recordset.getFields(colFields);
      long count = colFields.getCount();

      printf("\n6 Matched record:\n");
      for (long i = 0L; i<count; i++) {
        //Get an each ADOField object from colFields.
        ADOField field;
        if (colFields.getItem(i, field)) {
          _bstr_t name = field.getName();
          ADOEnumNames names;

          const char* type = names.getName(field.getType());
          _variant_t variant = field.getValue();
          
          _bstr_t value = "";
          COMTypeConverter converter;
          converter.toString(variant, value, _bstr_t("(null)"));
          _bstr_t attributes = "";
          field.getAttributes(attributes);

          printf("7 Name=%S, Type=%s, Attributes=%S, Value=%S, DefinedSize=%ld, ActualSize=%ld\n", 
              (const wchar_t*)name, 
              type,
              (const wchar_t*)attributes,
              (const wchar_t*)value,
              field.getDefinedSize(),
              field.getActualSize());
          
        }
      }

      recordset.moveNext();
    }
    counter.stop();
    printf("8 End: rencordset.walk, %ld(msec)\n", counter.getMilliseconds());


  }
};

}

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

    RecordsetFieldsApplet applet(argc, argv);

    applet.start();

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


Last modified: 2 May 2016

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