SOL9 Sample: RecordsetModifyBatchApplet

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * RecordsetModifyBatchApplet.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 RecordsetModifyBatchApplet: public ADOApplet {

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

public:
  ~RecordsetModifyBatchApplet()
  {
  }

public:
  /**
   * RecordsetModifyBatchApplet main procedure.
   * Demonstration for ADORecordset::updateBatch() and ADORecordset::requery().
   */
  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();
    } 

    //SQL statement: select 
    _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());

    ADOFields colFields;
    recordset.getFields(colFields);
    printf("5 recordset.getFields()\n");
    colFields.dump();
    long count = colFields.getCount();
    printf("6 colFields.getCount():%ld\n", count);

    _bstr_t company = "company";
    long companyIndex = colFields.getNameIndex(company);
    printf("7 OK colFields.getNameIndex(\"%S\"): %ld\n", (const wchar_t*)company, companyIndex);

    PerformanceCounter counter;

    counter.start();
    printf("8 Start: recordset walk\n");

    while(!recordset.getadoEOF()){
      printf("\n9 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("10 %S=%S\n", (const wchar_t*)name, (const wchar_t*)value);
        }
      }

      //Change "company" field to the following value.
      _bstr_t nvalue = L"Solar System Laboratory";
      recordset.putCollect(_variant_t(companyIndex), nvalue);
      printf("11 OK, recordset.putCollect(%ld, %S)\n", companyIndex, (const wchar_t*)nvalue);
      
      recordset.moveNext();
    }

    //Call UpdateBatch all changed recordset
    recordset.updateBatch(ADODB::adAffectAll);
    printf("12 OK, recordset.updateBatch(adAffectAll)\n");

    counter.stop();
    printf("13 End: rencordset.walk, Seconds %d\n", counter.getMilliseconds());

    recordset.requery(ADODB::adOptionUnspecified);
    printf("14 OK, recordset.requery()\n");

    while(!recordset.getadoEOF()){
      printf("\n15 Matched record:\n");
      for (long i = 0L; i<count; i++) {
        ADOField field;
        if (colFields.getItem(i, field)) {
          _bstr_t name = field.getName();
          _variant_t variant = recordset.getCollect(name);
          
          _bstr_t value = "";
          COMTypeConverter converter;
          converter.toString(variant, value);
          printf("16 %S=%S\n", (const wchar_t*)name, (const wchar_t*)value);
        }
      }
      recordset.moveNext();
    }    
  }
};

}

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