CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 33
  1. #16
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: VC++ Database Connection

    Quote Originally Posted by Suamere View Post
    Turns out it is impossible to use a Database with C++ unless you pay for Microsoft's VC++ 2008. Previous versions are no longer available for download from what I've read, and the express editions don't support MFC.

    In addition, it appears there is no way to connect to a database using another compiler such as Dev C++, since MFC is apparently the ONLY way to connect to a database.

    So sorry to all you C++ Wannabe developers who wanna use a database (the backbone of any application). You can't unless you buy VC++ 2008 from Microsoft.
    This is untrue. For one thing, any non-Express editions of Visual Studio will work. That includes VC6, VC 2002, VC 2003, and VC 2005.

  2. #17
    Join Date
    Jun 2006
    Posts
    645

    Re: VC++ Database Connection

    C++ is huge and I admit that it has a lot more steeper learning curve than any of the modern present day languages.
    Regarding your knowledge of Visual Basic, I am not sure if you are talking about classic VB (VB 6 and versions before that) or VB.NET. VB 6 is old and Microsoft has stopped encouraging any one from using classic VB that runs without .NET framework. But it still continues to support and improve up on its versions of Visual C++. Every edition of Visual Studio that comes out has a good amount of enhancements made for Visual C++.
    And if you are talking about VB.NET it is very much in use and is huge. Ofcourse, having an expertise of Vb helps but it has a lot more to it than classic VB. Also, if you are using VB.NET then it runs on .NET framework which also supports development in C# and C++ language. If you are developing windows apps, you can use managed C++ and follow the wizards provided by Visual Studio to connect to database.
    Regarding the edition problem, I would suggest you to d/l the trial versions for free from MSDN for learning purposes. You can d/l Visual Studio 2008 professional edition trial version that works for 90 days from MSDN which I believe would be enough for u to get a feel of the immense database connectivity support provided by MS for C++.
    It happens to everyone...banging ur head, tearing ur hair, throwing computer out of the window and all...But it occurs to be that u are not there yet. C++ means patience and extreme debugging. And of course, Codeguru forums. I have got 95% of help for my academic project which was a software developed using MS VC++ 2005 from Codeguru forums.
    Thank god TheCPUWizard did not see ur post, otherwise he would have first asked u to scrap out any older versions u are working on and get new ones. Arjay's example for database connectivity using ATL OLE DB is good one and it is only one way of doing things.

    Just a tip: You can use a good book for Visual C++ and turn to the database section. It has a step by step tutorial type explanation of how to connect to a database. There are plenty from Wrox, The Complete reference ones, MFC programming Unleashed, Visual Studio 2003 kick start and more....

    Regards,
    Bhushan

  3. #18
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: VC++ Database Connection

    Quote Originally Posted by Suamere View Post
    Turns out it is impossible to use a Database with C++ unless you pay for Microsoft's VC++ 2008. Previous versions are no longer available for download from what I've read, and the express editions don't support MFC.

    In addition, it appears there is no way to connect to a database using another compiler such as Dev C++, since MFC is apparently the ONLY way to connect to a database.

    So sorry to all you C++ Wannabe developers who wanna use a database (the backbone of any application). You can't unless you buy VC++ 2008 from Microsoft.
    That's just not true. You seem to have had an attitude of failure from the beginning. There are MFC classes that make it easiers, but MFC is not a requirement.

  4. #19
    Join Date
    Aug 2009
    Posts
    21

    Re: VC++ Database Connection

    Quote Originally Posted by GCDEF View Post
    That's just not true. You seem to have had an attitude of failure from the beginning. There are MFC classes that make it easiers, but MFC is not a requirement.
    Yes yes, I know. Arjay and you others are nice for reading and responding to my post. But though I haven't put years of work into "connecting to a database with C++) I have put a few hours a night for the last 3 weeks and am still unsuccessful.

    This thread has turned into an exact copy of every other 1,000,000 threads out there asking how to connect to a database. It has turned into vague comments and no resolve.

    I sound irate because it is literally impossible to get a database connection without paying for VC++ (year). Express edition is the only free edition, and previous versions are no longer available, since MS found out people want them.

    I'm mainly using Dev C++, and have created a pretty nice MUD just as my program for learning to develop in C++. I have everything working except a database, so when I shut down, everything is lost.

    So thanks for attempting to think you're helping, but vague "YES YOU CAN" comments don't make my program connect to a database.

    If this happens to be the last post, my current "hope" is that searching for "C++ ODBC Wrapper" can help me track down a solution, and perhaps in that wrapper is the secret to how to properly connect to a DB.

  5. #20
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: VC++ Database Connection

    Good grief dude. This took all of 30 seconds to find.

    http://www.easysoft.com/developer/la..._tutorial.html

  6. #21
    Join Date
    Aug 2009
    Posts
    21

    Re: VC++ Database Connection

    Quote Originally Posted by GCDEF View Post
    Good grief dude. This took all of 30 seconds to find.
    http://www.easysoft.com/developer/la..._tutorial.html
    Yes, that is helpful. Not sure what you searched for, but in all my searches I haven't found that. While it actually didn't work, it did lead me to something that did, with some tweaking.


    #include <windows.h>
    #include <iostream>
    #include <stdio.h>
    #include <sql.h>
    #include <sqlext.h>

    #define _countof(arr) (sizeof(arr)/sizeof(arr[0]))

    int main() {
    SQLHENV henv;
    SQLHDBC hdbc;
    SQLHSTMT hstmt;
    SQLRETURN retcode;
    SQLPOINTER rgbValue;
    int i = 5;
    rgbValue = &i;

    SQLCHAR OutConnStr[255];
    SQLSMALLINT OutConnStrLen;

    HWND desktopHandle = GetDesktopWindow(); // desktop's window handle

    // Allocate environment handle
    retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);

    // Set the ODBC version environment attribute
    if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
    retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER*)SQL_OV_ODBC3, 0);

    // Allocate connection handle
    if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
    retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);

    // Set login timeout to 5 seconds
    if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
    SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER)(rgbValue), 0);

    retcode = SQLDriverConnect( // SQL_NULL_HDBC
    hdbc,
    desktopHandle,
    (SQLCHAR*)"Driver={Microsoft Access Driver (*.mdb)}; DBQ=c:\\Database.mdb",
    _countof("Driver={Microsoft Access Driver (*.mdb)}; DBQ=c:\\Database.mdb"),
    OutConnStr,
    255,
    &OutConnStrLen,
    SQL_DRIVER_NOPROMPT );


    // Allocate statement handle
    std::cout << OutConnStr;
    if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
    retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
    std::cout << "Successfully Connected";
    system("PAUSE");
    // Process data
    if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
    SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
    }

    SQLDisconnect(hdbc);
    }else{
    fprintf(stderr, "Failed to Connect\n");
    system("PAUSE");
    }

    SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
    }
    }
    SQLFreeHandle(SQL_HANDLE_ENV, henv);
    }
    }
    I mentioned the prompt coming up, and I edited my code above to fix it, it simply required the NOPROMPT instead of PROMPT, lmao.

    Now I just have to figure out how to implement this into my programs and run SQL on it.
    Last edited by Suamere; August 29th, 2009 at 03:34 PM.

  7. #22
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: VC++ Database Connection

    Quote Originally Posted by Suamere View Post
    Please do not ask me questions. I don't care what compiler you tell me to use (VC++, Mac-Based, DevC++, etc)
    Quote Originally Posted by Suamere View Post
    Repeat: I don't CARE What setup your example is in..
    ...
    Quote Originally Posted by Suamere View Post
    I sound irate because it is literally impossible to get a database connection without paying for VC++ (year). Express edition is the only free edition, and previous versions are no longer available, since MS found out people want them.

  8. #23
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: VC++ Database Connection

    Quote Originally Posted by Suamere View Post
    I sound irate because it is literally impossible to get a database connection without paying for VC++ (year). Express edition is the only free edition, and previous versions are no longer available, since MS found out people want them.
    I doubt that Microsoft has anything to do with it. I don't know what country you are from, but if you are interested in programming, it just may be worth buying a copy of Visual Studio.

    Search google for "buy VC++ 2005 professional". I've found Visual Studio 2005 Standard Edition" for as low as $50.

    Look at it this way. You said you've spent 3 weeks trying to figure this stuff out only to find out that your free VS edition couldn't get the job done. If you spent just 2 hours a day and pay yourself a modest $10/hour, you've spent $300 worth of time over the past 3 weeks searching for a solution.

    That's not the worse of it. Consider what will happen if you have to program using the antiquated ODBC api. Using the raw api, it's going to take you 10 times as long to get anything done compared to using a non-express VS edition and being able to use some modern classes like MFC, ADO, ATL OLEDB an so on.

    If you are a hobbyist, that's fine, but if you are serious about a career in software development, then it might be worth investing in a copy of Visual Studio. It might even make you more attractive in the job market as well, if you had more up date technologies under your belt.

  9. #24
    Join Date
    Aug 2009
    Posts
    21

    Re: VC++ Database Connection

    Thanks Martin, lol. I've posted on other forums playing "yes sir" and all those threads turned into nothing. By presenting you guys with a challenge I finally got a response that started to help me get into the general area by which I could find help. Good enough for me, though.

    Hey, since you're all here, do you happen to know what this code set is called? I am an SQL master, but SQL seems so dirty in C++. When I look it up, I get a lot of MFC results and some conn->Execute stuffs that wouldn't work with the code I'm using.

    I've looked into SQLDirectExec and SQLExecute, and will eventually figure it out. But what is this code set called? Is it called SQLExt?

    My next goal is to use Stored Procedures in my Access Database.

  10. #25
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: VC++ Database Connection

    Quote Originally Posted by Suamere View Post
    Thanks Martin, lol. I've posted on other forums playing "yes sir" and all those threads turned into nothing. By presenting you guys with a challenge I finally got a response that started to help me get into the general area by which I could find help. Good enough for me, though.

    Hey, since you're all here, do you happen to know what this code set is called? I am an SQL master, but SQL seems so dirty in C++. When I look it up, I get a lot of MFC results and some conn->Execute stuffs that wouldn't work with the code I'm using.

    I've looked into SQLDirectExec and SQLExecute, and will eventually figure it out. But what is this code set called? Is it called SQLExt?

    My next goal is to use Stored Procedures in my Access Database.
    SQL isn't 'dirty' in C++ if you don't program with 20 year old technology. I can create a SQL connection and call a SQL stored procedure in about 5 minutes in C++. Can you do that with the raw SQLDirect et al, api's?

    Last I knew stored procedures weren't available in Access. What version have they become available?

  11. #26
    Join Date
    Aug 2009
    Posts
    21

    Re: VC++ Database Connection

    Quote Originally Posted by Arjay View Post
    Last I knew stored procedures weren't available in Access. What version have they become available?
    Since forever:
    http://images.devshed.com/af/stories...les/adop02.jpg

    You just have to manage the database with a server.

    I see where there was confusion, though. I suppose I could just ask in general how to call stored procedures with C++.

  12. #27
    Join Date
    Jun 2006
    Posts
    645

    Re: VC++ Database Connection

    Good that this thread got you going. Just a thought, looking at your extent of expertise with C++, I would really suggest you to grab a decent book for a specific compiler you are using and then turn to the database section. After, you have worked with 2-3 compilers or environments and database connectivity techniques, you will yourself find it easier to learn ahead. Also, you might not need a book for your subsequent attempts to connect to a database. It is only for the starters. To sustain in the current market, you need to know a handful of these.
    Goodluck,
    Bhushan.

  13. #28
    Join Date
    Aug 2009
    Posts
    21

    Re: VC++ Database Connection

    I bought bibles for PHP, .NET, MCDBA, and other books that I've used over the years. I'd do the same for C++ (Dev C++ preferably based on what I've seen and used so far), but I don't even have money for a piece of candy. I can buy bread, milk, gas, and that's about it right now. But I'll get one eventually.

  14. #29
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: VC++ Database Connection

    Quote Originally Posted by Suamere View Post
    I bought bibles for PHP, .NET, MCDBA, and other books that I've used over the years. I'd do the same for C++ (Dev C++ preferably based on what I've seen and used so far), but I don't even have money for a piece of candy. I can buy bread, milk, gas, and that's about it right now. But I'll get one eventually.
    That's odd. In your original post you said "I'll get the compiler you used, on the platform you used it on, with all the setup you have, and I'll use that for the rest of my life if I have to."

  15. #30
    Join Date
    Aug 2009
    Posts
    21

    Re: VC++ Database Connection

    Ya. So if somebody said "use this, do this, and this works like this." I'd have done it. What came out of that was the link you posted to a tutorial that linked to something that worked with DevC++. So that's what worked, that's what I'm using.

    So I made this DB Connection a function that I call at the beginning of my program, and now whenever I wanna do SQL, how do I do it? I can do SQL within the function like so:

    strcpy((char *) SQLStmt, "Select etc...");
    retcode = SQLExecDirect(hstmt, SQLStmt, SQL_NTS);

    SQLBindCol(hstmt, 1, SQL_C_CHAR, &rtnCellValue1, sizeof(rtnCellValue1), NULL );

    for(;; ){
    retcode = SQLFetch(hstmt);
    if (retcode == SQL_NO_DATA_FOUND) break;
    std::cout << rtnPassword << " - " << rtnCellValue1 << endl;
    }
    That works fine after the SQLDriverConnect within the function, but how do I do that like... in a new function? Do I have to make hstmt global? (Tried that). I dunno how to make the connection useable throughout the program.

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured