A Simple and Smart ODBC Wrapper Library


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

  • Microsoft Visual C++ 6.0/SP4
  • ODBC 3.0 or above
  • Microsoft Windows 95...XP

    Direct usage of ODBC is very complex. Anybody who tries to work with ODBC can say it. Also, ODBC is a function-level interface. It is a good universal interface, but all structured C++ applications look very horrible when plain ODBC calls are used.

    I want to offer a little library, which:

    • Wraps access to ODBC into a set of classes
    • Wraps processing of ODBC and usage errors into an exception mechanism
    • Doesn't use any external libraries, including MFC and ATL
    • Doesn't require any special conditions to use it
    • Automates column binding operations
    • Can automate (for MSSQL server) binding of parameters
    • Can provide transparent work with LOB columns/parameters without limitation of their size
    • Has a Help file, even if this Help file is simple
    • Is freeware, open-source library
    • Supports powerful local-side cursors with indexing and modifying of cursor(insert, update, delete) are supported

    I sought such a library for some years, but I didn't find a library that conforms to all these conditions. Because I have used ODBC in many projects, some classes were developed some time ago. Now I have refined these classes. And I hope that these classes can help somebody.


    Download source - ~140 Kb
    Contains sources of library and Help files.

    Download demo project - ~70 Kb
    Contains project with a demonstration of the library usage.

  • Comments

    • 64bit or Windows 7 issue

      Posted by georgicus.fruticis on 01/28/2010 11:25am

      Hey Nick, we adapted your code a long time ago to access a MSAccess database. Now we are forced to switch to 64Bit Windows XP/7. For unknown reasons the execution of prepared statements with binary parameters don't set the binary values. ODBC gives me no error so I'm stuck. Any hint will be welcome. Greetings, Georgicus

    • Simple Example

      Posted by auron on 02/07/2006 08:05pm

      Great library. If you are a novice to ODBC/Database programming it might throw you off at first if you compare it to other libraries. Here are a couple examples to get you started:
      I used MySQL to make a simple table called test with only two columns. You need to bind the columns manually first. 
      CGOdbcConnect cCon;
      cCon.connect("database", "username", "password");
      CGOdbcStmt *pCur;
      pCur = cCon.createStatement();
      Cur->prepare("insert into test values (?,?)");
      pCur->bindParam(0, SQL_INTEGER, SQL_C_LONG, 4, 0, 0);
      pCur->bindParam(1, SQL_CHAR, SQL_C_CHAR, 33, 32, 0);
      pCur->setChar(1,"it worked!");
      say you want to do a simple SELECT from a database you can just make a SQL statement, autobind, and get your values:
      CGOdbcConnect cCon;
      cCon.connect("database", "user", "password");
      CGOdbcStmt *pCur;
      pCur = cCon.createStatement();
      pCur->execute("SELECT * FROM articles");
      htmlPage = pCur->getChar("title");
      htmlPage += pCur->getChar("body");
      This library implements SQLPutData so you can post large amounts of data to your database. Understanding ODBC programming is a pain, but this code is cleanly written and using ibm and microsoft help articles you can use and extend these classes.
      I couldn't find any other resource that inmplements SQLPutData in a way that could be used immediately, but understood later. 
      Thanks again.

      • How to choose a MS Access file from such an example?

        Posted by founderideas01 on 03/23/2008 11:24pm

        Hi, Auron, I would like to choose a MS ACCess database from such an example, how to do it? In ODBC Microsoft access setup, in "User DSN" tab, if I connect my MS access file with Data Source name "MS Access 97 Database", I can run your example ( adapted with my case): int _tmain(int argc, _TCHAR* argv[]) { CGOdbcConnect cCon; cCon.connect("MS Access 97 database", "Admin", ""); CGOdbcStmt *pCur; pCur = cCon.createStatement(); pCur->execute("select * from Table1"); pCur->bindAuto(); pCur->first(); const char * htmlPage = pCur->getChar("Symbol"); const char * htmlPage1 = pCur->getChar("Event"); cCon.freeStatement(pCur); cCon.close(); return 0; } But, how can I choose my Access database file, from these lines of code instead of manually with the ODBC Access setup? Thanks a lot. Founder

    • hWnd in CGOdbcConnect::connect(const char *szStr, HWND hWnd) goes nowhere

      Posted by AngusEm on 07/20/2005 03:31am

      It's a pity that this project isn't better documented, since it's much simpler to use that any other free ODBC API out there. There's also a problem w/the CGOdbcConnect::connect() when the driver needs to open a dialogue box w/the user: the hWnd is not used, and 0 is used in its place. I corrected the problem by changing: gsodbc_src/connect.cpp (line 76) rc = SQLDriverConnect(m_hConn, 0, (unsigned char *)szStr, SQL_NTS, to rc = SQLDriverConnect(m_hConn, hWnd, (unsigned char *)szStr, SQL_NTS, I'm not sure why that 0 is there. It looks deliberate, but my fix hasn't turned up any problems for me yet.

    • Memory leaks???

      Posted by Legacy on 01/14/2004 08:00am

      Originally posted by: Jarle Thu

      Has anyone else had any problems with memory leaks using gsodbc?
      I'm no expert c++ programmer (which is why I use this library :-) but something is making my memory usage go sky high :(
      Just to check it I changed the demo app to do just logon/logoff in a loop.. this increases memory usage several 100k's for each pass..
      I've tried this with different odbc drivers and databases(ingres, MySQL)..

      Also in my "real" application it doesn't seems like memory is freed when I call
      after doing a DB querey, it only seems to free 4k...

      Anyone else had this problem or knows a workaround??

      Any help appreciated!


    • Excellent, the most well structured example I've seen in a long time.

      Posted by Legacy on 11/12/2003 08:00am

      Originally posted by: Jake

      I have used this library for some time now and I love it. It was wonderful to say goodbye to ADO as attempting to figure out what MS screwed up on was a nightmare.

      I give this code 5 *'s

    • Spectacular

      Posted by Legacy on 08/11/2003 07:00am

      Originally posted by: Adrian


    • How to insert data to BLOB field..?

      Posted by Legacy on 02/27/2003 08:00am

      Originally posted by: Keun

      Hi, all

      I try to insert data at BLOB field

      ora-12571 error

    • Updating with cursors

      Posted by Legacy on 02/24/2003 08:00am

      Originally posted by: Levi Browde

      Updates using the cursor are not saving to the database. When I run the following code:

      CGCursor * cCursor = new CGCursor( pCur);
      cCursor->go( j );
      char str[256];
      cCursor->setChar( "acct_name" , str);

      After the "setChar," a subsequent "getChar" indicates that the new value in "str" took, but the change is not committed to the database.

      I am able to update this field using the same ODBC Source via a query tool.

      Am I missing something obvious?

    • Alias problem

      Posted by Legacy on 02/12/2003 08:00am

      Originally posted by: Lechuck

      I've got problem when I'm using alias in sql like :

      select value*10 as v from tablename;

      Sql state:=00000

      Any idea how to solve this problem?

    • Static linking?

      Posted by Legacy on 02/05/2003 08:00am

      Originally posted by: Stefan

      Is it possible to link this library into my exe? If yes, how to do it?

    • Loading, Please Wait ...

    • You must have javascript enabled in order to post comments.

    Leave a Comment
    • Your email address will not be published. All fields are required.

    Most Popular Programming Stories

    More for Developers

    RSS Feeds

    Thanks for your registration, follow us on our social networks to keep up-to-date