SOL9 Sample: UpdateTableApplet

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


// SOL9
// 2009/05/20
// Assumes that environment of SQL Server 2008 and SQLClient1.0 
// Please create a database 'Sample' for this example

/*
In this sample, we assume the following table exists:

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/Locale.h>

namespace SOL {

class UpdateTableApplet: public ADOApplet {

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

public:
  ~UpdateTableApplet()
  {
  }

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

    //SQL statements
    const wchar_t* sqls[] = {
      L"Update SolUser set Email='williams@solarsystemlaboratory.com@earth.sol' where ID = 1",
      L"Update SolUser set Email='thomas@marslaboratory.marsme@mars.sol' where Name like 'Thomas%'"};
 
    connection.beginTrans();
    printf("2 OK, connection.beginTrans()\n");

    for (int i = 0; i<sizeof(sqls)/sizeof(sqls[0]); i++) {
      ADORecordset recordset;
      try {
        long recordsAffected = connection.execute(_bstr_t(sqls[i]), recordset);
        printf("3 OK, connection.execute(\"%S\")\n",
          sqls[i]);
        printf("4 Updated record count(RecordsAffected) = %ld\n", recordsAffected);

      } catch (_com_error& e) {
        COMError error(e);
        error.dump();
      } catch (...) {
        printf("Exception:Unknown\n");
      }
    }
    connection.commitTrans();
    printf("5 OK, connection.commitTrans()\n");
  }
};

}


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

    UpdateTableApplet updateTableApplet(argc, argv);

    updateTableApplet.start();

  } catch(Exception& ex){
    ex.dump();
  } catch(...){
    printf("Exception:Unknown\n");
  }
  printf("\nHit return key!\n");
  getchar();
}

Last modified: 2 May 2016

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