Step by Step TimesTen-- 使用ODBC訪問TimesTen資料庫C++篇(一個例子)(1)
1,在client端配置ODBC
參見文件:Step by Step TimesTen ----- 配置client-server連線
http://space.itpub.net/81/viewspace-421428
2,設定環境變數
ODBCINI=/ora/TimesTen/odbc.ini
SYSTTCONNECTINI=/ora/TimesTen/ttconnect.ini
3,程式:
test.cc
#ifdef WIN32
#include
#else
#include
#endif
#include
#include
#include
#include
#include
void CheckReturnCode(SQLRETURN rc, SQLHENV henv,
SQLHDBC hdbc, SQLHSTMT hstmt,
char* msg, char *filename,
int lineno);
int main(int argc, char** argv)
{
SQLRETURN rc = SQL_SUCCESS;
/* General return code for the API */
SQLHENV hEnv = SQL_NULL_HENV;
/* Environment handle */
SQLHDBC hDbc = SQL_NULL_HDBC;
/* Connection handle */
SQLHSTMT hStmt = SQL_NULL_HSTMT;
/* Statement handle */
SQLCHAR ConnOut[255];
/* Buffer for completed connection string */
SQLSMALLINT connOutLen;
/* number of bytes returned in ConnOut */
SQLCHAR *ConnString = (SQLCHAR *)
"DSN=test_cs";
/* Connection attributes */
rc = SQLAllocEnv(&hEnv);
if (rc != SQL_SUCCESS) {
fprintf(stderr,"Unable to allocate an "
"environment handle\n");
exit(1);
}
rc = SQLAllocConnect(hEnv, &hDbc);
CheckReturnCode(rc, hEnv, SQL_NULL_HDBC,
SQL_NULL_HSTMT,
"Unable to allocate a "
"connection handle\n",
__FILE__, __LINE__);
#if 1
rc = SQLDriverConnect(hDbc, NULL,
ConnString, SQL_NTS,
ConnOut, 255,
&connOutLen,
SQL_DRIVER_NOPROMPT);
#else
rc = SQLConnect(hDbc, ConnString,
SQL_NTS,(SQLCHAR*)"", SQL_NTS,
(SQLCHAR*)"", SQL_NTS);
printf("\n\nSQLConnect():%d\n",rc);
#endif
CheckReturnCode(rc, hEnv, hDbc, SQL_NULL_HSTMT,
"Error in connecting to the"
" driver\n",
__FILE__, __LINE__);
rc = SQLAllocStmt(hDbc, &hStmt);
CheckReturnCode(rc, hEnv, hDbc, SQL_NULL_HSTMT,
"Unable to allocate a "
"statement handle\n",
__FILE__,__LINE__);
/* Your application code here */
if (hStmt != SQL_NULL_HSTMT)
{
rc = SQLFreeStmt(hStmt, SQL_DROP);
CheckReturnCode(rc, hEnv, hDbc, hStmt,
"Unable to free the "
"statement handle\n",
__FILE__, __LINE__);
}
if (hDbc != SQL_NULL_HDBC)
{
rc = SQLDisconnect(hDbc);
CheckReturnCode(rc, hEnv, hDbc,
SQL_NULL_HSTMT,
"Unable to close the "
"connection\n",
__FILE__, __LINE__);
rc = SQLFreeConnect(hDbc);
CheckReturnCode(rc, hEnv, hDbc,
SQL_NULL_HSTMT,
"Unable to free the "
"connection handle\n",
__FILE__, __LINE__);
}
if (hEnv != SQL_NULL_HENV)
{
rc = SQLFreeEnv(hEnv);
CheckReturnCode(rc, hEnv, SQL_NULL_HDBC,
SQL_NULL_HSTMT,
"Unable to free the "
"environment handle\n",
__FILE__, __LINE__);
}
}
void CheckReturnCode(SQLRETURN rc, SQLHENV henv,
SQLHDBC hdbc, SQLHSTMT hstmt,
char* msg, char *filename,
int lineno)
{
#define MSG_LNG 512
SQLCHAR szSqlState[MSG_LNG];
/* SQL state string */
SQLINTEGER pfNativeError;
/* Native error code */
SQLCHAR szErrorMsg[MSG_LNG];
/* Error msg text buffer pointer */
SQLSMALLINT pcbErrorMsg;
/* Error msg text Available bytes */
SQLRETURN ret = SQL_SUCCESS;
if (rc != SQL_SUCCESS && rc != SQL_NO_DATA_FOUND )
{
if (rc != SQL_SUCCESS_WITH_INFO)
{
/*
* It's not just a warning
*/
fprintf(stderr, "*** ERROR in %s, line %d:"
" %s\n",
filename, lineno, msg);
}
/*
* Now see why the error/warning occurred
*/
while (ret == SQL_SUCCESS ||
ret == SQL_SUCCESS_WITH_INFO)
{
ret = SQLError(henv, hdbc, hstmt,
szSqlState, &pfNativeError,
szErrorMsg, MSG_LNG,
&pcbErrorMsg);
blog長度限制,無法貼完全部程式碼,接下篇Step by Step TimesTen-- 使用ODBC訪問TimesTen資料庫C++篇(一個例子)(2)http://space.itpub.net/81/viewspace-438948
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/81/viewspace-438946/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Step by Step TimesTen-- 使用ODBC訪問TimesTen資料庫C++篇(一個例子)(2)資料庫C++
- Step by Step TimesTen --- ttIsqlSQL
- Step by Step TimesTen --- DataStore的雙向複製( 一)AST
- 資料庫設計 Step by Step (1)——揚帆啟航資料庫
- Step by Step TimesTen ----- 配置client-server連線clientServer
- Step by Step TimesTen --- DataStore的雙向複製( 二)AST
- Learn c++ step by step (轉)C++
- Learn C++ step by step(2) (轉)C++
- 透過RMAN進行資料庫恢復(step by step)資料庫
- TIDB DM資料同步step by stepTiDB
- STREAMS筆記(1) step by step 建立一個Streams複製環境筆記
- 使用RMAN備份集搭建Oracle Dataguard Step by Step(一)Oracle
- 利用VPD細粒度訪問策略實現行級安全性 Step By Step
- 轉載一個step by step change public-ip and vip on RAC
- React Step by StepReact
- Git Step by Step (6):Git遠端倉庫Git
- 2.4.14 Step 13: 備份資料庫資料庫
- 2.4.10 Step 9:手工建立資料庫資料庫
- ClearCase使用入門--step by step(序) (轉)
- Step by Step, 為OSRFX2建立一個KMDF驅動程式
- Git Step by Step (7):Git遠端倉庫(續)Git
- oracle10g simpe AQ step by step(一)Oracle
- 一個用功能強大的ODBC API 函式訪問資料庫類 (轉)API函式資料庫
- Command 模式 Step by Step模式
- BAPI Step by step GuidanceAPIGUI
- 阿里雲數倉Dataworks資料匯出到檔案step by step阿里
- step1 補充
- Vue.js SSR Step by Step (2) – 一個簡單的同構DEMOVue.js
- Vue.js SSR Step by Step (2) - 一個簡單的同構DEMOVue.js
- 直接透過ODBC API訪問SQL資料庫 (轉)APISQL資料庫
- 使用RMAN備份集搭建Oracle Dataguard Step by Step(二)Oracle
- Promise的實現(step by step)Promise
- Oracle 11gR2 Active DataGuard配置Step By Step(一)Oracle
- GoldenGate<一> step by step installation and configurationGo
- 使用RMAN備份集搭建Oracle Datagard Step by Step(三)Oracle
- Linux Software RAID step by stepLinuxAI
- Git Step by Step (3):Git物件模型Git物件模型
- Oracle高階複製Step by StepOracle