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?
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?
This is what i've managed so far:
Am i on the right path? I receive -1 when i try to SQLAllocHandle(SQL_HANDLE_DBC)..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); }
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.
That worked, thanks!
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
I have VC++, is ADO faster?
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....