SOL9 Sample: ParametrizedQueryApplet
|
1 Screenshot
2 Source code
/*
* ParametrizedQueryApplet.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 {
class ParametrizedQueryApplet: public ADOApplet {
public:
/**
* Constructor
*/
ParametrizedQueryApplet(int argc, const TCHAR** argv)
:ADOApplet(argc, argv)
{
}
public:
~ParametrizedQueryApplet()
{
}
public:
/**
* ParametrizedQueryApplet main procedure
*/
virtual void run()
{
printf("1 Start\n");
ADOConnection connection = getConnection();
ADOCommand command;
command.createInstance();
//Parametrized query string.
_bstr_t query("SELECT * FROM SolUser WHERE Age>? AND Nationality =?");
command.putActiveConnection(connection.getConnectionPtr());
command.putCommandText(query);
command.putCommandType(ADODB::adCmdText);
command.putCommandTimeout(20);
//Set Prepared property to be true
command.enablePrepared(true);
printf("2 OK, putCommandText(\"%S\")\n", (const wchar_t*)query);
// Create an ADOParameter Object from arguments of appendToParameters
// and apppend it to ADOParameters of ADOCommand object command
command.appendParameter("@Age", ADODB::adInteger, ADODB::adParamInput,
sizeof(int), _variant_t(106));
//This indicates that "where Age >106"
printf("3 OK, command.appendToParameters()\n");
// Create an ADOParameter Object from arguments of appendToParmeters
// and apppend it to ADOParameters of ADOCommand object command
command.appendParameter("@Nationality", ADODB::adVarChar, ADODB::adParamInput,
20, _variant_t("USA"));
//This indicates that "where Nationality ='USA'"
printf("4 OK, command.appendToParameters()\n");
ADOParameters params;
command.getParameters(params);
printf("5 OK, command.getParameters()\n");
params.dump();
ADORecordset recordset;
long recordsAffected = command.execute(recordset,
NULL, ADODB::adCmdText);
printf("6 OK, command.execute(\"%S\") recordsAffected(%ld)\n",
(const wchar_t*)command.getCommandText(), recordsAffected);
while(!recordset.getadoEOF()){
printf("\n7 Matched record:\n");
ADOFields colFields;
recordset.getFields(colFields);
long count = colFields.getCount();
for (long i = 0L; 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("8 %S=%S\n", (const wchar_t*)name, (const wchar_t*)value);
}
}
recordset.moveNext();
}
}
};
}
// Console application starts here.
void _tmain(int argc, const TCHAR** argv)
{
Locale locale;
try {
ParametrizedQueryApplet queryApplet(argc, argv);
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.