title: C++ SQLDriverConnect API
body: <p>I am using visual studio 2008 and sql server 2008 for developing application(SQL server is in my system). I need to fetch some fields from the database. I am using the SQLDriverConnect API to connect to the database.</p>
  
  <p>If I use the "SQL_DRIVER_PROMPT" I will get pop window to select the data source. I don't want this window to appear. As per my understanding this window will appear if we provide insufficient information in the connection string. I think I have provided all the information. I am trying to connect with windows authentication. I tried different options but still no luck. Please help me in solving this problem.</p>
  
  <p>Below is the code that I am using:</p>
  
  <pre><code>//********************************************************************************
  // SQLDriverConnect_ref.cpp
  // compile with: odbc32.lib user32.lib
  #include &lt;windows.h&gt;
  #include &lt;sqlext.h&gt;
  
  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, &amp;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, &amp;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, 
             &amp;OutConnStrLen,
             SQL_DRIVER_NOPROMPT); 
  
  
              // Allocate statement handle
              if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {               
                 retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &amp;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);
     }
  }
  
  //********************************************************************************
  </code></pre>
  
  <p>Thanks in advance,
  Harsha</p>