SOL9 Sample: PreparedQueryApplet

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * PreparedQueryApplet.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 {

/**
 * ADO applet subclass derived from SOL::ADOApplet class.
 */
class PreparedQueryApplet: public ADOApplet {

private:
  bool prepared;
  
public:
  /**
   * Constructor
   */
  PreparedQueryApplet(int argc, const TCHAR** argv)
    :ADOApplet(argc, argv)
  {
    prepared = false;
  }

public:
  ~PreparedQueryApplet()
  {
  }

public:
  void enablePrepared(const TCHAR* flag)
  {
    if (_stricmp(flag, _T("true")) == 0) {
      prepared = true;
    } else {
      prepared = false;
    }
  }

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

    ADOCommand command;
    command.createInstance();
    command.putActiveConnection(connection.getConnectionPtr());
    command.putCommandText(_bstr_t("SELECT * FROM SolUser WHERE Age>20 AND Age<50 AND Nationality='USA'"));
       command.putCommandType(ADODB::adCmdText);
    command.putCommandTimeout(20);
    //Set Prepared property to be true or false
    command.enablePrepared(prepared);

    ADORecordset recordset;
    long recordsAffected = command.execute(recordset, NULL, ADODB::adCmdText);
    printf("2 OK, command.execute(\"%S\") recordsAffected(%ld)\n",
       (const wchar_t*)command.getCommandText(), recordsAffected);

    PerformanceCounter counter;
    counter.start();

    while(!recordset.getadoEOF()){
      printf("\n3 Matched record:\n");
      ADOFields colFields;
      recordset.getFields(colFields);
      long count = colFields.getCount();

      for (long i = 0; i<count; i++) {
        ADOField field;
        if (colFields.getItem(i, field)) {
          _bstr_t name = field.getName();

          _variant_t variant = field.getValue();
          _bstr_t value = "";
          COMTypeConverter converter;
          converter.toString(variant, value);
          printf(" %S=%S\n", (const wchar_t*)name, (const wchar_t*)value);
        }
      }  
      recordset.moveNext();
    }
    counter.stop();
    printf("4 Prepared Query(%d): Milliseconds %d took to do '%S'\n", 
      prepared,
      counter.getMilliseconds(),
      (const wchar_t*)command.getCommandText());
    
  }
};

}

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

  if (argc != 3) {  
    printf("Usage:%s <Connection.props file> <prepared(true/false)>\n", argv[0]);
    printf("%s Connection.props true\n", argv[0]);
    return;
  }

  try {

    PreparedQueryApplet queryApplet(argc, argv);
    queryApplet.enablePrepared(argv[2]);  
    queryApplet.start();

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


Last modified: 2 May 2016

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