September 28th, 2003, 03:39 AM
C++ DB integration
I have an application written in C++ and i would like to add a DB.
I am using MS-Accsess (but it can be oralce later on).
I use VS 6 .I already connected the DB to my project(on the ODBC
level),but i need to know which classes/functions/includes i can
use to send sql commands from my project and my DB
and how to make my project "know" that it is using my specific DB.
P.s - i DON'T want to use the AppWiz!!!
September 28th, 2003, 10:26 AM
Here is some code that uses ADO and DSN-less connections. I have abstracted the database connection a bit for ease of use (it will do its own connection the first time it is used). I only gave you the barest of running example (it is using Access 97), but it should give you the ideas you need for a more complex example.
I hope you find it useful.
September 28th, 2003, 03:05 PM
September 28th, 2003, 03:08 PM
Thanks for the reply -it helped me a lot.
I have only two questions:
1)How do I find out what to write for oracle in the "establishing
the new connection" row in the code you sent me?
2)If I understand correctly the program establishes a connection
to the DB,executes the sql command and dissconnects.
Is it possible/Where can I change to add more than one
September 28th, 2003, 04:32 PM
The dbaccess class will hang around as long as you keep it in scope. I usually create it in my main() and pass it around as a pointer, that way it stays in place until the program exits. You can call Execute() as many times as you want. As for the Oracle connection string, google for 'dsn-less connection' and 'oracle' and possibly 'ado' (though with dsn-less connection you probably don't need it). You can also create a DSN via the ODBC setup applet (in Control Panel or Administrative Tools) and use that instead. I prefer the DSN-less connection because I don't have to do the ODBC setup on each machine it runs on. Just for comparison, here is the connection string for SQLServer:
"Provider=sqloledb;Data Source=<machinename>;Initial Catalog=<databasename>;User Id=<usergoeshere>;Password=<passgoeshere>;"
September 29th, 2003, 07:55 AM
Thank you! I've been looking for a simple example like this for a while (okay, not really looking, more just keeping my eye out for one).
September 29th, 2003, 08:37 AM
I am glad to offer it. It represents a lot of grubbing around on the internet to find bits and peices of code. I find that MS's examples are often too cute and make the code difficult to read and maintain. The example I presented is 'inefficient' in that there is no preparsing of the SQL. If it is called many times there is a significant loss in efficiency, but since computers are so damn fast now adays, I find the ease of maintenance to far outweigh the loss of efficiency. However, I do feel obligated to point out that the method presented in the example (in-line SQL) is subject to sql injection hack attacks. It is probably a very bad idea to use this technique where ever your code available from unsecured machines. Under those circumstances you either need to use the object approach (which has the benefit of preparsing the code) or write your own SQL verifier (not what I would recommend, too many variables). The object approach (which, unfortunately I don't have any examples of in ADO) registers veriables in the SQL statement and is pretty much immune from sql injection. It loses some flexibilty and easy of maintenance, but I have used that tradeoff in code that made heavy use of TEXT with success. I have also used it to call stored procedures. That way I also have the ability to change the way the program behaves by modifying a stored procedure and can leave the exe alone.
September 30th, 2003, 12:49 AM
By stored procedures you mean a predefined sql statments in a
sql file which is called upon request by the exe?
I tried expending your code to have more than one sql query by
calling the execute() several times and changing the value of
strSQL and pRst but i only see the first query being executed.
Could you reply on where should i change those values and call
the execute() ,and how can i call the stored procedure from the
September 30th, 2003, 03:35 AM
Show me your code (be sure to enclose in "code" tags!). Once you do the execute, you need to assign a recordset pointer to the result set, then read through the result set. In other words, you need to duplicate almost all the code in the main() routine to get at another recordset (pretty much everthing in the try/catch code). It is about the minimum amount of code you can write (and do error checking) to get at the data.
As for stored procedures, that depends on support of the underlying database. Oracle had them I am sure. I think Access does as well, though I tend to avoid its use so don't have any example code (the vast bulk of my SQL is with Sybase and MS SQLServer).