SQLData Systems, Inc.
Home | Products | Services | Documents | Support


This sample demonstrates how easy it is to create a dynamic query processor using the ODBC Broker. The program is a console application, which accepts a connection string as the argument and prompts for a query statement to be processed.

/* 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",
        return 0;

        printf("%s\n", pBroker->GetHeader());
        long rows=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;
        // some thing wrong, get the error string
        printf("Error open query, %s\n",
    // we are done
    return nTotalRows;

// this is a little test program for our Query function.
main(int argc, char **argv)
    char szSQL[500];
    {   // assume our name is query.exe.
        printf("Usage: query.exe DataSource\n");

        printf("Type in your sql statement.\n");
        int nRows=Query(argv[1], szSQL);
        printf("\n %d rows returned.\n", nRows);
    return 0;

Special Note:

bulletException handling is omitted for simplicity.
bulletThe Query function makes a database connection
whenever it processes a query statement. This could be
time consuming. A better way is to have the broker
connected to the database only once, and use the
same connection for all the SQL statements.

This can be done if the Broker is created in the
main function and move the database connection
from ExecuteSQL to main().


Download | Purchase | ContactFeedback

horizontal rule

Send mail to  info2-at-sqldata-dot-com with questions or comments about this web site.
Copyright 2008-2010 SQLData Systems, Inc.
Last modified: July 10, 2010