SOL9 2.0 Sample: InsertIntoTableApplet

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * InsertIntoTableApplet.cpp 
 * Copyright (c) 2009 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>

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();

        //SQL statements 
        const wchar_t* sqls[] ={
            L"Insert into SolUser (ID, Name, Email) values(1, 'Williams Bone', 'bill@antillia.terranet@earth.sol')",
            L"Insert into SolUser (ID, Name, Email) values(2, 'Thomas Hanks',  'tom@antillia.starnet@moon.earth.sol')",
            L"Insert into SolUser (ID, Name, Email) values(3, 'Perry V. Rodan', 'perry@antillia.starnet@earth.sol')",
            L"Insert into SolUser (ID, Name, Email) values(4, '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)
{
    try {

        InsertIntoTableApplet insertIntoTableApplet(argc, argv);

        insertIntoTableApplet.start();

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

Last modified: 11 Nov 2009

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