Visual Studio C++连接到DB2

问题描述:

我正在尝试使用数据库编写C++程序 - IBM DB2 Express-C。另外,我使用Visual Studio 2015.通过IBM OLE DB Provider for DB2,我的Visual Studio成功连接到DB2。我可以看到表和一切OK:Visual Studio C++连接到DB2

enter image description here

然后创建新的C++程序中,我成功地连接几个头文件,一些从他们身上我在DB2目录中,例如sqlcli1.h

#pragma once 

#include "targetver.h" 

#include <stdio.h> 
#include <tchar.h> 
#include <stdlib.h> 
#include <string.h> 
#include <sqlcli1.h> 
#include <windows.h> 

以下是主程序的代码。其中我正尝试连接到成功连接到Visual Studio的数据库。

#include "stdafx.h" 


int main() 
{ 
    SQLHENV henv; 
    SQLHDBC hdbc; 
    SQLHSTMT hstmt; 
    SQLRETURN retcode; 

    SQLCHAR * OutConnStr = (SQLCHAR *)malloc(255); 
    SQLSMALLINT * OutConnStrLen = (SQLSMALLINT *)malloc(255); 

    // Allocate environment handle 
    retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv); 

    // Set the ODBC version environment attribute 
    if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { 


     // Allocate connection handle 
     if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { 
      retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc); 




       // Connect to data source 
      retcode = SQLConnect(hdbc, (SQLWCHAR*) "DATABASE=SAMPLE;HOSTNAME=localhost", SQL_NTS, (SQLWCHAR*) "db2admin", SQL_NTS, (SQLWCHAR*) "password", SQL_NTS); 
       if (retcode != SQL_SUCCESS) { 
        printf(">--- Error while connecting to database:"); 
        printf("SQLConnect: %d\n", retcode); 
       } 
       // Allocate statement handle 
       if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { 
        retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt); 
        printf("\nAllocate Connection handle successfully."); 
        // Process data 
        if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { 
         SQLFreeHandle(SQL_HANDLE_STMT, hstmt); 
        } 

        SQLDisconnect(hdbc); 
       } 

       SQLFreeHandle(SQL_HANDLE_DBC, hdbc); 

     } 
     SQLFreeHandle(SQL_HANDLE_ENV, henv); 
    } 
    system("pause"); 
    return 0; 
} 

在编译时没有错误,但无法连接到数据库。当程序执行到达块时。

// Connect to data source 
      retcode = SQLConnect(hdbc, (SQLWCHAR*) "DATABASE=SAMPLE;HOSTNAME=localhost", SQL_NTS, (SQLWCHAR*) "db2admin", SQL_NTS, (SQLWCHAR*) "password", SQL_NTS); 
       if (retcode != SQL_SUCCESS) { 
        printf(">--- Error while connecting to database:"); 
        printf("SQLConnect: %d\n", retcode); 
       } 

我看到> ---错误,同时连接到数据库:的SQLConnect:-2 这是什么意思?我在做什么是错误的?我也试过了:

retcode = SQLConnect(hdbc, (SQLWCHAR*) "SAMPLE", SQL_NTS, (SQLWCHAR*) "db2admin", SQL_NTS, (SQLWCHAR*) "password", SQL_NTS); 

但它也行不通。可能是第二个参数应该看起来像其他方式?

改变 我想利用这个功能SQLGetDiagRec的我创建了一些变量

SQLWCHAR  SqlState[6], SQLStmt[100], Msg[SQL_MAX_MESSAGE_LENGTH]; 
    SQLINTEGER NativeError; 
    SQLSMALLINT i, MsgLen; 
    SQLRETURN  rc1, rc2; 

我也改变我的连接块

// Connect to data source 
      retcode = SQLConnect(hdbc, (SQLWCHAR*) "SAMPLE", SQL_NTS, (SQLWCHAR*) "db2admin", SQL_NTS, (SQLWCHAR*) "password", SQL_NTS); 
       if (retcode != SQL_SUCCESS) { 
        printf(">--- Error while connecting to database:"); 
        printf("SQLConnect: %d\n", retcode); 

        if ((retcode == SQL_SUCCESS_WITH_INFO) || (retcode == SQL_ERROR)) { 
         // Get the status records. 
         i = 1; 
         while ((rc2 = SQLGetDiagRec(SQL_HANDLE_STMT, hdbc, i, SqlState, &NativeError, 
          Msg, sizeof(Msg), &MsgLen)) != SQL_NO_DATA) { 
          cout << SqlState; 
          cout << i; 
          cout << NativeError; 
          cout << Msg; 

          i++; 
         } 
        } 
       } 

但一切都没有改变。有人有什么主意吗?

如果对任何CLI功能的调用不返回SQL_SUCCESS,则错误处理应使用SQLGetDiagRec()来获取警告或错误的详细信息。

文档:SQLGetDiagRec

+0

我试了一下**改变的问题**,但功能'SQLGetDiagRec的()'不打印任何东西。 – Vladimir