2010-01-07 4 views
2

J'utilise Visual Studio 2008 et SQL Server 2008 pour développer une application (le serveur SQL est dans mon système). J'ai besoin de récupérer des champs de la base de données. J'utilise l'API SQLDriverConnect pour me connecter à la base de données.API C++ SQLDriverConnect

Si j'utilise le "SQL_DRIVER_PROMPT" j'obtiendrai une fenêtre pop pour sélectionner la source de données. Je ne veux pas que cette fenêtre apparaisse. Selon ma compréhension, cette fenêtre apparaîtra si nous fournissons des informations insuffisantes dans la chaîne de connexion. Je pense avoir fourni toutes les informations. J'essaie de me connecter avec l'authentification Windows. J'ai essayé différentes options mais toujours pas de chance. S'il vous plaît aidez-moi à résoudre ce problème.

est Ci-dessous le code que j'utilise:

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

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

Merci à l'avance, Harsha

Répondre

3

Utilisez SQLConnect pour se connecter; il ne prévoit pas d'incitation.

Oups! Mauvaise idée. Selon the documentation for SQLDriverConnect, SQLConnect accepte uniquement: DSN, UID et PW.

Vous devez passer le pointeur NULL pour le hwnd, lors de l'appel de SQLDriverConnect, si vous ne voulez pas autoriser l'invite. Spécifiez SQL_DRIVER_NOPROMPT pour l'option d'invite (comme vous le faites). Si l'appel échoue, examinez le code de réponse de sortie et imprimez les messages.

Voilà comment vous pouvez faire les contrôles:

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

Et voici comment vous invoquez l'appel:

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