SOL9 Sample: SQLServerAuthApplet

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


// SOL9
// 2009/05/19
// Assumes that environment of SQL Server 2008 and SQLClient1.0 
// Please create a database 'Sample' for this example
// and following table 'Person'.
/*
CREATE TABLE Person(
UserId int,
Telephone varchar(128),
Email varchar(255),
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City    varchar(255));
*/

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

namespace SOL {

class SQLServerAuthApplet: public ADOApplet {

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

public:
  ~SQLServerAuthApplet()
  {
  }


public:
  /**
   * SQLSeverAuthApplet 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("Person"), 
        connection.getConnectionPtr(), colFields); 
    long count = colFields.getCount();
    printf("2 OK, colFields.getCount(): %d\n", count);

    //SQL statement: select 
    _bstr_t query("SELECT * FROM Person");

    ADORecordset recordset;
    long recordsAffected = connection.execute(query, recordset);
    printf("3 OK, connection.execute(\"%S\")\n", (const wchar_t*)query);
    printf("4 RecordsAffected=%ld\n", recordsAffected);
    
    while(!recordset.getadoEOF()){
      printf("\nMatched 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("5 %S=%S\n", (const wchar_t*)name, (const wchar_t*)value);
        }
      }  
      recordset.moveNext();
    }  
  }
};

}

//
void _tmain(int argc, const TCHAR** argv)
{
  Locale locale;
  try {

    SQLServerAuthApplet authApplet(argc, argv);
        
    authApplet.start();

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

Last modified: 2 May 2016

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