SOL9 Sample: RecordsetModifyApplet

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


// SOL9
// 2009/05/28
// 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 RecordsetModifyApplet: public ADOApplet {

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

public:
  ~RecordsetModifyApplet()
  {
  }

public:
  /**
   * RecordsetModifyApplet 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();
    } 

    ADOCommand command;
    command.createInstance();

    ADOFields colFields;
    command.getFields(_bstr_t("SolUser"), 
        connection.getConnectionPtr(), colFields); 
    long count = colFields.getCount();
    printf("2 OK, colFields.getCount(): %d\n", count);

    //SQL statement: select. Sort by age
    _bstr_t query("SELECT * FROM SolUser WHERE Age>30 AND Age<35 ORDER by AGE");

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

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

    //Gettting an index for "company" field of SoUser table.
    _bstr_t company("company");
    long companyIndex = colFields.getNameIndex(company);//_bstr_t("company"));

    PerformanceCounter counter;

    counter.start();
    printf("6 Start: recordset walk\n");
  
    while(!recordset.getadoEOF()){
      printf("\n7 Matched record:\n");
      for (long i = 0L; i<count; i++) {
        ADOField field;
        if (colFields.getItem(i, field)) {
          _bstr_t name = field.getName();

          //Call recordset.getCollect(name);
          _variant_t variant = recordset.getCollect(name);
          
          _bstr_t value = "";
          COMTypeConverter converter;
          converter.toString(variant, value);
          printf("8 %S=%S\n", (const wchar_t*)name, (const wchar_t*)value);    
        }
      }

      //Change Company field to a some value.
      recordset.putCollect(_variant_t(companyIndex), _bstr_t(L"Marsnet.com"));
      printf("9 OK, recordset.putCollect()\n");

      //Update chanted recordset
      recordset.update();
      printf("10 OK, recordset.update()\n");

      recordset.moveNext();
    }
    counter.stop();
    printf("11 End: recordset walk. Performance=%d(msec)\n", counter.getMilliseconds());
  }
};

}

// Console application starts here.
void _tmain(int argc, const TCHAR** argv)
{
  Locale locale;
  try {
    RecordsetModifyApplet 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.