/* the Query function is a dynamic query processor. It requires two arguments: an ODBC
data source name (DSN) and an SQL Query statement. The query result is printed out on the
screen. The DSN can also be a connect string in a format of DSN=DataSourceName;UID=YourUserId;PWD=YourPassword*/
long Query(const char * szDataSource, const char * szSQL)
{
int nTotalRows=0;
Broker *pBroker =CreateBroker(Broker::DATA_BROKER, NULL);
// make a database connection , without user name and pwd
if(!pBroker->Connect(szDataSource, "", ""))
{
// we have trouble connecting to the database
printf("Unable to connect to the database
%s\n",
szDataSource);
return 0;
}
if(pBroker->OpenSQL(szSQL))
{
printf("%s\n",
pBroker->GetHeader());
long rows=0;
while((rows=pBroker->Fetch())>0)
{
// we fetched n rows
each time.
for(int i=0; i<rows;
i++)
{ // print
out each record.
printf("%s\n", pBroker->GetRecord(i));
}
nTotalRows +=rows;
}
}
else
{
// some thing wrong, get the error string
printf("Error open query, %s\n",
pBroker->GetErrorString());
}
// we are done
DestroyBroker(pBroker);
return nTotalRows;
}
// this is a little test program for our Query function.
main(int argc, char **argv)
{
char szSQL[500];
if(argc!=2)
{ // assume our name is query.exe.
printf("Usage: query.exe
DataSource\n");
}
while(1)
{
printf("Type in your sql
statement.\n");
gets(szSQL);
int nRows=Query(argv[1], szSQL);
printf("\n %d rows returned.\n",
nRows);
}
return 0;
}
Special Note: