C ++ SQLDriverConnect API

时间:2010-01-07 03:34:25

标签: visual-c++ database-connection

我正在使用visual studio 2008和sql server 2008开发应用程序(SQL server在我的系统中)。我需要从数据库中获取一些字段。我正在使用SQLDriverConnect API连接到数据库。

如果我使用“SQL_DRIVER_PROMPT”,我将弹出窗口来选择数据源。我不希望这个窗口出现。根据我的理解,如果我们在连接字符串中提供的信息不足,则会出现此窗口。我想我已经提供了所有的信息。我正在尝试连接Windows身份验证。我尝试了不同的选择,但仍然没有运气。请帮我解决这个问题。

以下是我正在使用的代码:

//********************************************************************************
// SQLDriverConnect_ref.cpp
// compile with: odbc32.lib user32.lib
#include <windows.h>
#include <sqlext.h>

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

   SQLWCHAR OutConnStr[255];
   SQLSMALLINT OutConnStrLen;
   SQLCHAR      ConnStrIn[255] =
       "DRIVER={SQL Server};SERVER=(local);DSN=MyDSN;DATABASE=MyDatabase;Trusted_Connection=yes;";

   //SQLWCHAR *ConntStr =(SQLWCHAR *) "DRIVER={SQL Server};DSN=MyDSN;";
   HWND desktopHandle = GetDesktopWindow();   // desktop's window handle

   // 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) {
      retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER*)SQL_OV_ODBC3, 0); 

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

         // Set login timeout to 5 seconds
         if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
            SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER)5, 0);

         retcode = SQLDriverConnect( // SQL_NULL_HDBC
           hdbc,
           desktopHandle,
       (SQLWCHAR *)ConnStrIn,
       SQL_NTS,
           OutConnStr,
           255, 
           &OutConnStrLen,
           SQL_DRIVER_NOPROMPT); 


            // Allocate statement handle
            if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {               
               retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt); 

               // 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);
   }
}

//********************************************************************************

提前致谢, 戒日

1 个答案:

答案 0 :(得分:3)

使用SQLConnect进行连接;它没有提供提示。

糟糕!馊主意。根据{{​​3}},SQLConnect仅接受:DSN,UID和PW。

如果您不想允许提示,则应在调用SQLDriverConnect时传递hwnd的NULL指针。为提示选项指定SQL_DRIVER_NOPROMPT(就像那样)。如果调用失败,请检查输出响应代码,并打印出消息。

以下是您可以进行检查的方法:

boolean CHECK_STATUS(SQLRETURN rc, UCHAR *location, SQLHANDLE hnd, SQLSMALLINT hType)
{
    if (rc == SQL_SUCCESS)
    {
        printf("  ** %s: ok\n", location);
    }
    else
    {
        UCHAR *status = (rc == SQL_SUCCESS_WITH_INFO) ? "OK" : "failed" ;
        SQLRETURN _rc;
        UCHAR state[5+1];
        SQLINTEGER ecode;
        SQLSMALLINT msgLength;
        int recNumber = 0;

        printf("  ** %s: %s. rc(%d)\n", location, status, rc);

        do {
            _rc = SQLGetDiagRec(hType,
                                hnd,
                                ++recNumber, // RecNumber,
                                state,       // SQLState,
                                &ecode,      //    NativeErrorPtr,
                                NULL,        //      MessageText,
                                0,           //   BufferLength,
                                &msgLength);
            if (_rc == SQL_NO_DATA) {}
            else if (_rc != SQL_SUCCESS) {
                printf("    ** Cannot retrieve error message, reason: %d\n", _rc);
            }
            else {
                if (recNumber == 1)
                    printf("  ** SQL State: %s\n", state);
                if (rc != SQL_SUCCESS_WITH_INFO)
                    printf("  ** error code: %d\n", ecode);
                //printf("  ** %smsg length: %d\n", (rc != SQL_SUCCESS_WITH_INFO)?"error " : "", msgLength);
                if (msgLength > 0)
                {
                    UCHAR * msg = malloc((msgLength+2) * sizeof(UCHAR));
                    _rc = SQLGetDiagRec(hType,
                                        hnd,
                                        recNumber,   // RecNumber,
                                        state,       // SQLState,
                                        &ecode,      //    NativeErrorPtr,
                                        msg,         //      MessageText,
                                        msgLength+2, //   BufferLength,
                                        &msgLength);

                    printf("  ** msg %d: %s\n", recNumber, msg);
                    free(msg);
                }
            }
        } while (_rc == SQL_SUCCESS);
    }

    return (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO);
}


boolean CHECK_STATUS_DBC(SQLRETURN rc, UCHAR *location, SQLHANDLE hnd)
{
    return CHECK_STATUS(rc, location, hnd, SQL_HANDLE_DBC);
}


boolean CHECK_STATUS_ENV(SQLRETURN rc, UCHAR *location, SQLHANDLE hnd)
{
    return CHECK_STATUS(rc, location, hnd, SQL_HANDLE_ENV);
}

以下是你调用电话的方式:

SQLRETURN rc;
UCHAR   *szDSN = "Driver={SQL Server};Server=hostname\\SQLEXPRESS;Database=Northwind;Trusted_Connection=Yes;";
HDBC    hdbc1 = SQL_NULL_HDBC;
HSTMT   hstmt1 = SQL_NULL_HSTMT;
boolean mustFreeConnect = FALSE;
boolean mustDisconnect = FALSE;
SQLSMALLINT dwLength;

printf("\nHello from OdbcEx1. \n");

printf("\nWill connect to %s\n", szDSN);

rc = SQLAllocEnv (&henv);
CHECK_STATUS_ENV(rc, "SQLAllocEnv", henv);

// Set the flavor of ODBC to 3.0
rc = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3, 0);
CHECK_STATUS_ENV(rc, "SQLSetEnvAttr", henv);

rc = SQLAllocConnect(henv, &hdbc1);
mustFreeConnect = CHECK_STATUS_DBC(rc, "SQLAllocConnect", henv);

rc = SQLDriverConnect(hdbc1,
                      NULL,
                      szDSN,
                      SQL_NTS,
                      NULL,
                      0,
                      &dwLength,
                      SQL_DRIVER_NOPROMPT);
mustDisconnect = CHECK_STATUS_DBC(rc, "SQLConnect", hdbc1);