This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
|
dev:odbc:examples:simpleselect [2009/12/06 03:00] tdo created |
dev:odbc:examples:simpleselect [2016/06/28 22:38] (current) |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| {{page>:top_add&nofooter&noeditbtn}} | {{page>:top_add&nofooter&noeditbtn}} | ||
| <html><div align="center"><span style="color:red">DRAFT</span></div></html> | <html><div align="center"><span style="color:red">DRAFT</span></div></html> | ||
| + | ====== C++ Omnidex ODBC Simple Connection/SELECT ====== | ||
| + | ^ [[dev:home | Dev ]] ^ [[dev:odbc:examples:simpleselect | C++ ]] ^ [[dev:odbc:examples:csharp_simple_select |C# ]] ^ [[dev:jdbc:examples:simpleselect | Java ]] ^ [[dev:odbc:examples:php_select | PHP ]] ^ | ||
| + | ===== Quick Notes ===== | ||
| + | |||
| + | * SQLConnect can only use System and Machine datasources so use SQLDriverConnect. | ||
| + | * SQLDriverConnect can use a Omnidex File DSN with the FILEDSN= prefix. | ||
| + | * Make sure odxodbc.dll is in the c:\Windows\system32 directory | ||
| + | * Make sure an OdxNet process is running | ||
| + | |||
| + | ===== C++ Sample Code ===== | ||
| + | You can cut and paste this code and modify the the SQLDriverConnection call and the SELECT statement to test on your Omnidex Environment. | ||
| + | |||
| <code C++> | <code C++> | ||
| #include "stdafx.h" | #include "stdafx.h" | ||
| Line 16: | Line 28: | ||
| SQLHDBC hdbc; | SQLHDBC hdbc; | ||
| SQLHSTMT hstmt; | SQLHSTMT hstmt; | ||
| - | SQLRETURN retcode; | ||
| char errormsg[255] = "x"; | char errormsg[255] = "x"; | ||
| SQLCHAR * OutConnStr = (SQLCHAR * )malloc(255); | SQLCHAR * OutConnStr = (SQLCHAR * )malloc(255); | ||
| Line 23: | Line 34: | ||
| cout << "OdxODBCConnectionTest" << endl; | cout << "OdxODBCConnectionTest" << endl; | ||
| // Allocate environment handle | // Allocate environment handle | ||
| - | retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv); | + | if (SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv) != SQL_SUCCESS) |
| - | cout << "Alloc ENV retcode:" << retcode << endl; | + | cout << "sQLAllocHandle (ENV) Error" << endl; |
| - | retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC2, 0); | + | if (SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC2, 0) != SQL_SUCCESS) |
| - | cout << "Set Env retcode: " << retcode << endl; | + | cout << "SQLSetEnvAttr Error" << endl; |
| - | retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc); | + | if (SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc) != SQL_SUCCESS) |
| - | cout << "Alloc hdbc retcode: " << retcode << endl; | + | cout << "SQLAllocHandle (DBC) Error" << endl; |
| - | retcode = SQLDriverConnect(hdbc, NULL, | + | if (SQLDriverConnect(hdbc, NULL, |
| (SQLCHAR*) "FILEDSN=c:\\dev\\odx\\tiny\\tiny.dsn", SQL_NTS, | (SQLCHAR*) "FILEDSN=c:\\dev\\odx\\tiny\\tiny.dsn", SQL_NTS, | ||
| - | (SQLCHAR*) NULL, 0, NULL, 0); | + | (SQLCHAR*) NULL, 0, NULL, 0) != SQL_SUCCESS) |
| - | cout << "Connect retcode:" << retcode << endl; | + | { |
| - | if (retcode != SQL_SUCCESS) | + | cout << "SQLDriverConnect Error:" << endl; |
| - | { | + | // If we get to here, display the SQLError message text to see what’s going on. |
| - | retcode = SQLError(henv, hdbc, NULL, NULL , NULL, (SQLCHAR*) errormsg, sizeof(errormsg), NULL); | + | SQLError(henv, hdbc, NULL, NULL , NULL, (SQLCHAR*) errormsg, sizeof(errormsg), NULL); |
| - | cout << errormsg << endl; | + | cout << errormsg << endl; |
| - | } | + | |
| - | retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt); | + | |
| - | cout << "Alloc stmt:" << retcode << endl; | + | |
| - | retcode = SQLExecDirect(hstmt, (SQLCHAR*) "select mytext from table1 where mytext='bears'", SQL_NTS); | + | |
| - | cout << "Exec Direct retcode:" << retcode << endl; | + | |
| - | SQLCHAR mytext[60]; | + | |
| - | retcode = SQLBindCol(hstmt, 1, SQL_C_CHAR, (SQLPOINTER) &mytext, | + | |
| - | sizeof(mytext), NULL); | + | |
| - | while (retcode==0) | + | |
| - | { | + | |
| - | retcode = SQLFetch(hstmt); | + | |
| - | cout << mytext << endl; | + | |
| } | } | ||
| + | if (SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt) != SQL_SUCCESS) | ||
| + | cout << "SQLAllocHandle (STMT) Error" << endl; | ||
| + | if (SQLExecDirect(hstmt, | ||
| + | (SQLCHAR*) "select mytext from table1 where mytext='bears'", SQL_NTS) != SQL_SUCCESS) | ||
| + | cout << "ExecDirect Error" << endl; | ||
| + | SQLCHAR mytext[60]; // Declare varable for the Bind. | ||
| + | if (SQLBindCol(hstmt, 1, SQL_C_CHAR, (SQLPOINTER) &mytext, | ||
| + | sizeof(mytext), NULL) != SQL_SUCCESS) | ||
| + | cout << "SQLBindCol Error" << endl; | ||
| + | while (SQLFetch(hstmt) == SQL_SUCCESS) | ||
| + | cout << mytext << endl; | ||
| SQLFreeHandle(SQL_HANDLE_STMT, hstmt); | SQLFreeHandle(SQL_HANDLE_STMT, hstmt); | ||
| - | retcode = SQLDisconnect(hdbc); | + | if (SQLDisconnect(hdbc) != SQL_SUCCESS) |
| + | cout << "SQLDisconnect Error" << endl; | ||
| SQLFreeHandle(SQL_HANDLE_DBC, hdbc); | SQLFreeHandle(SQL_HANDLE_DBC, hdbc); | ||
| SQLFreeHandle(SQL_HANDLE_ENV, henv); | SQLFreeHandle(SQL_HANDLE_ENV, henv); | ||
| cout << "End of program." << endl; | cout << "End of program." << endl; | ||
| } // end main | } // end main | ||
| + | |||
| </code> | </code> | ||
| {{page>:bottom_add&nofooter&noeditbtn}} | {{page>:bottom_add&nofooter&noeditbtn}} | ||