ODBC connection not using MFC

This is a discussion on ODBC connection not using MFC within the Windows Programming forums, part of the Platform Specific Boards category; Hi. I'm experimenting a little with database connection, and i wonder how i can connect to an ODBC dsn from ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    329

    ODBC connection not using MFC

    Hi.
    I'm experimenting a little with database connection, and i wonder how i can connect to an ODBC dsn from my c++ program, not using MFC?
    Any tips?

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    This is what i've managed so far:
    Code:
    short odbcConn(){
    	char buf[10];
    	SQLHANDLE henv; // Environment Handle 
    	short sRes;
    
    	if((sRes=SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv)) == SQL_SUCCESS || sRes == SQL_SUCCESS_WITH_INFO){
    		if((sRes=SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc)) == SQL_SUCCESS || sRes == SQL_SUCCESS_WITH_INFO){
    			if((sRes=SQLConnect(hdbc, (unsigned char*)"scanfisk", SQL_NTS, (unsigned char*)"dba", SQL_NTS, (unsigned char*)"sql", SQL_NTS)) == SQL_SUCCESS || sRes == SQL_SUCCESS_WITH_INFO){
    				;
    			}else{sprintf(buf, "SQLConnect=%i", sRes);b(NULL, buf);}
    		}else{sprintf(buf, "SQL_HANDLE_DBC=%i", sRes);b(NULL, buf);}
    	}else{sprintf(buf, "SQL_HANDLE_ENV=%i", sRes);b(NULL, buf);}
    
    	return(0);
    }
    Am i on the right path? I receive -1 when i try to SQLAllocHandle(SQL_HANDLE_DBC)..

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Have a look at this, hopefully it will point you in the right direction. Obviously, the names and fields will need changing.

    Code:
     #include <SQL.H>
     #include <SQLEXT.H>
     void CAddressBookView::OnFillListBox() 
     {
       RETCODE rcode;
    
       HENV henv1;
       HDBC hdbc1;
       HSTMT hstmt1;
    
       char szFirstName[50];
       char szLastName[50];
       char szPhoneNum[20];
    
       SDWORD sdODataLength;
       unsigned char conStringOut[256];
    
       rcode = ::SQLAllocEnv(&henv1);
       if (rcode == SQL_SUCCESS)
       {
         rcode = ::SQLAllocConnect(henv1, & hdbc1);
         if (rcode == SQL_SUCCESS)
         {
             rcode = ::SQLDriverConnect(hdbc1, 0,
             (unsigned char *)"DSN=AddressBookDb", 
             SQL_NTS, conStringOut, 256, NULL, 
             SQL_DRIVER_NOPROMPT);
           if (rcode == SQL_SUCCESS)
           {
             rcode = ::SQLAllocStmt(hdbc1, &hstmt1);
             if (rcode == SQL_SUCCESS)
             {
                 rcode = ::SQLExecDirect(hstmt1, 
                 (unsigned char *)<$I~ODBC API;applications, developing;simple example 
    listing><$I~applications;ODBC API;simple example listing><$I~developing;ODBC API applications;simple example 
    listing><$I~listings;ODBC example to retrieve last name from AddressBook database><$I~ODBC;example to retrieve 
    last name from AddressBook database;listing 14.1>
                 "SELECT szLastName FROM AddressTable",
                 SQL_NTS);
    
               for (rcode = ::SQLFetch(hstmt1);
                 rcode == SQL_SUCCESS;
                 rcode = SQLFetch(hstmt1))
               {
                 ::SQLGetData(hstmt1, 1, SQL_C_CHAR, 
                   szLastName, 50,  & sdODataLength);
                 ::MessageBox(NULL, szLastName, 
                   " from AddressBookDb ", MB_OK);
               }
               ::SQLFreeStmt(hstmt1, SQL_DROP);
             }
             ::SQLDisconnect(hdbc1);
           }
           ::SQLFreeConnect(hdbc1);
         }
         ::SQLFreeEnv(henv1);
       }
     }
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    That worked, thanks!

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Also if you have VC++, have a look at ADO.....with that compiler's #import directive, you can have all the objects & constants for the library created for you...no need for MFC

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    I have VC++, is ADO faster?

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    It's pretty fast...you can use ADO from ASP and stuff (via a dispatch interface), but if you use the method I suggested, then it's pretty fast..

    Also, it sits upon OleDB...and so in the future will be a lot more commonplace than ODBC....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ODBC DSN-less connection
    By esaptonor in forum Windows Programming
    Replies: 8
    Last Post: 06-14-2007, 11:09 PM
  2. ODBC Connection
    By StephanC in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 01:08 AM
  3. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  4. WIndows programming?
    By hostensteffa in forum Windows Programming
    Replies: 7
    Last Post: 06-07-2002, 08:52 PM
  5. ODBC connection
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 09-07-2001, 08:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21