C++ 应用程序连接到InterSystems IRIS数据库 - 使用 ODBC
连接前准备:
- C++ 开发环境
- InterSystems ODBC 驱动 (ODBC 驱动会随InterSystems IRIS安装包自动安装在服务器中)
- Connection String
步骤:
- Connection String:其中#include 用来导入需要使用的libraries,"Driver=InterSystems IRIS ODBC35;Host=localhost;Port=1972;Database=USER;UID=SQLAdmin;PWD=deployment-password;\0";是connection string。
#ifdef WIN32 #include <windows.h> #endif #include <sqlext.h> #ifdef UNICODE #include <sqlucode.h> #endif #include <stdio.h> int main() { RETCODE rc; /* Return code for ODBC functions */ HENV henv = NULL; /* Environment handle */ HDBC hdbc = NULL; /* Connection handle */ unsigned char szOutConn[600]; SQLSMALLINT *cbOutConn = 0; SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv); SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER*)SQL_OV_ODBC3, 0); SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc); char connect_cmd[255] = "Driver=InterSystems IRIS ODBC35;Host=localhost;Port=1972;Database=USER;UID=SQLAdmin;PWD=deployment-password;\0"; rc = SQLDriverConnect(hdbc, NULL, (SQLCHAR*) connect_cmd, SQL_NTS, szOutConn, 600, cbOutConn, SQL_DRIVER_COMPLETE); if (rc == SQL_SUCCESS) { printf("Successfully connected!!\n"); } else { printf("Failed to connect to IRIS\n"); exit(1); } SQLDisconnect(hdbc); SQLFreeHandle(SQL_HANDLE_DBC, hdbc); /* Free connection handle */ SQLFreeHandle(SQL_HANDLE_ENV, henv); /* Free environment handle */ return 0; }