#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    33
    Rep Power
    11

    Exclamation C++ DB integration


    Hello

    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!!!

    Many thanks

    Jake
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    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.
    Attached Files

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    33
    Rep Power
    11
    Thanks a lot!!!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    33
    Rep Power
    11
    Dear mitakeet

    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

    command?

    Once again

    Many thanks

    Jake
  8. #5
  9. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    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:

    Code:
    "Provider=sqloledb;Data Source=<machinename>;Initial Catalog=<databasename>;User Id=<usergoeshere>;Password=<passgoeshere>;"

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  10. #6
  11. ASP.Net MVP
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Aug 2003
    Location
    WI
    Posts
    4,378
    Rep Power
    1510
    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).
    Primary Forum: .Net Development
    Holy cow, I'm now an ASP.Net MVP!

    [Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]

    http://twitter.com/jcoehoorn
  12. #7
  13. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    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.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    33
    Rep Power
    11
    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

    main?

    many thanks
  16. #9
  17. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    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).

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

IMN logo majestic logo threadwatch logo seochat tools logo