hlAdo

hlAdo is a library to access any database from within your C++ programs. It uses the Ado COM to succeed database connectivity. This library was designed for use with Visual C++ version 6 SP5 or higher.

hlAdo was designed with simplicity in mind. We want to open databases and query them in a simple and easy way. To achieve simplicity for example, you can create the class instances without using pointers. This way, you don’t have to release them, avoiding possible bugs. Also the library uses either CStrings or char* as parameters to function calls, so it is up to you to choose what type of strings you will use.

Take a brief look at this simple code:


hlAdo hl("Provider=Microsoft.JET.OLEDB.3.51;Data Source=DB.mdb");

hlAdoRs rs(hl,"SELECT names,phones FROM Directory");
while (rs.next()) // while there are records...
{
      // ... print them
     cout << (LPCTSTR)rs.GetItem("names") << "\t" << (LPCTSTR)rs.GetItem("phones") << endl;
}

For a quick introduction to the library, please open the example which is included in the hlAdo.zip. (Open the hlAdo_Demo.dsw)

Download the library with a full example(hlAdo.zip , 84kb) / Download the documentation in word 2000 format

The library is free to use for non-commercial purposes, but please donate if you include it in a commercial project. See donation link at the bottom of the page.

To use the library, you need two files: hlAdo.h and hlAdo.cpp added to your multithreaded project.

Also do:

#include <hlAdo.h>

to every cpp file that uses the library, or to your stdafx.h, and make sure you got _WIN32_DCOM defined (put it in your "preprocessor definitions" of your project).

Connecting to the database

hlAdo hl(connection_string);

Connection string examples:

"Provider=Microsoft.JET.OLEDB.3.51;Datasource=access_97_Database.mdb"

“dsn=data_source_name;UID=login;PWD=Password”

Executing an Update or Delete command

hl.ExecuteCommand("UPDATE / DELETE query");

Executing query (without using pointer – recommended)

hlAdoRs rs(hl,"SELECT query");
while (rs.next())
{
      cout << (LPCTSTR)rs.GetItem("column name") << endl;
      … …
}

Executing a query (using pointers version)

hlAdoRs *rs=hl.Execute("SELECT query");

while (rs->next())
{
      // GetItem returns a CString
      cout << (LPCTSTR)rs->GetItem("column name") << endl;
      ………
      ………
}
delete rs;

Transactions

hl.BeginTrans();
hl.CommitTrans();
hl.RollbackTrans();

Catching errors

try
{
      ADO CALLS
}
AdoCatch(e)
{
      cout << (LPCTSTR)ComError(e) << endl;
}

Declerations

HlAdo:

      // Transaction functions
      void RollbackTrans();
      void CommitTrans();
      void BeginTrans();
      // Mostly used internally to create an hlAdoRs
      _RecordsetPtr Execute_(CString & sql);
      _RecordsetPtr Execute_(char * sql);
      // constructor. Pass the connection string.
      hlAdo(CString &constr);
      hlAdo(char* constr);
      // destructor
      ~hlAdo();
      // return the conection pointer
      _ConnectionPtr GetConnection();
      // execute a query which returns data, i.e. select
      hlAdoRs * Execute(CString &sql);
      hlAdoRs * Execute(char* sql);
      // execute a query which doesn't return data i.e. update
      void ExecuteCommand(CString &sql);
      void ExecuteCommand(char* sql);

hlAdoRs:

      // the recommended constructor. You shall have already
      // made the connection to the DB with an hlAdo object.
      hlAdoRs(hlAdo &hl,CString &sql);
      hlAdoRs(hlAdo &hl,char *sql);
      // get an item of a row.
      CString GetItem(CString &ColumnName);
      CString GetItem(char *ColumnName);
      // move to the next row.
      bool next();
      // the constructor used by hlAdo->Execute to create this
      // object.
      hlAdoRs(_RecordsetPtr rsp,hlAdo *hlado);
      // destruction
      ~hlAdoRs();

Utility functions:

CString ComError(_com_error &e);

 If you find this library useful, please support me by donating via paypal. Thanks.