SOL9 Sample: InsertIntoTableApplet

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * InsertIntoTableApplet.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

// Please run CreateTableApplet.exe before running this program.

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

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

public:
  ~InsertIntoTableApplet()
  {
  }

public:
  /**
   * InsertIntoTableApplet main procedure
   */
  virtual void run()
  {
    printf("1 Start\n");
    ADOConnection connection = getConnection();
    _bstr_t database = "Sample";
    try {
      _bstr_t useDatabase = "Use " + database;

      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"Insert into SolUser (ID, Age, Name, Email) values(1, 27, 'Williams Bone', 'bill@antillia.terranet@earth.sol')",
      L"Insert into SolUser (ID, Age, Name, Email) values(2, 35, 'Thomas Hanks',  'tom@antillia.starnet@moon.earth.sol')",
      L"Insert into SolUser (ID, Age, Name, Email) values(3, 22, 'Perry V. Rodan', 'perry@antillia.starnet@earth.sol')",
      L"Insert into SolUser (ID, Age, Name, Email) values(4, 33, 'Zimmy J. Hant', 'zimmy@antillia.marsnet@mars.sol')"};

    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 Inserted records 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 {

    InsertIntoTableApplet insertIntoTableApplet(argc, argv);

    insertIntoTableApplet.start();

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

Last modified: 2 May 2016

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