title: Using ODBC to connect to SQL SERVER 2008
body: <p>I'm on a W32 machine trying to get a connection with a DB. For that purpose, I tried starting testing an example program:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;windows.h&gt;
  #include &lt;sqltypes.h&gt;
  #include &lt;sql.h&gt;
  #include &lt;sqlext.h&gt;
  
  using namespace std;
  
  void show_error(unsigned int handletype, const SQLHANDLE&amp; handle){
      SQLCHAR sqlstate[1024];
      SQLCHAR message[1024];
      if(SQL_SUCCESS == SQLGetDiagRec(handletype, handle, 1, sqlstate, NULL, message, 1024, NULL))
          cout&lt;&lt;"Message: "&lt;&lt;message&lt;&lt;"\nSQLSTATE: "&lt;&lt;sqlstate&lt;&lt;endl;
  }
  
  int main(){
  
      SQLHANDLE sqlenvhandle;
      SQLHANDLE sqlconnectionhandle;
      SQLHANDLE sqlstatementhandle;
      SQLRETURN retcode;
  
      if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &amp;sqlenvhandle))
          goto FINISHED;
  
      if(SQL_SUCCESS!=SQLSetEnvAttr(sqlenvhandle,SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0))
          goto FINISHED;
  
      if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_DBC, sqlenvhandle, &amp;sqlconnectionhandle))
          goto FINISHED;
  
      SQLCHAR retconstring[1024];
      switch(SQLDriverConnect (sqlconnectionhandle,
                  NULL,
                  (SQLCHAR*)"DRIVER={SQL Server};SERVER=localhost, 1433;DATABASE=MyDatabase;UID=sa;PWD=Admin-123;",
                  SQL_NTS,
                  retconstring,
                  1024,
                  NULL,
                  SQL_DRIVER_NOPROMPT)){
          case SQL_SUCCESS_WITH_INFO:
              show_error(SQL_HANDLE_DBC, sqlconnectionhandle);
              break;
          case SQL_INVALID_HANDLE:
          case SQL_ERROR:
              show_error(SQL_HANDLE_DBC, sqlconnectionhandle);
              goto FINISHED;
          default:
              break;
      }
  
      if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_STMT, sqlconnectionhandle, &amp;sqlstatementhandle))
          goto FINISHED;
  
      if(SQL_SUCCESS!=SQLExecDirect(sqlstatementhandle, (SQLCHAR*)"select * from testtable", SQL_NTS)){
          show_error(SQL_HANDLE_STMT, sqlstatementhandle);
          goto FINISHED;
      }
      else{
          char name[64];
          char address[64];
          int id;
          while(SQLFetch(sqlstatementhandle)==SQL_SUCCESS){
              SQLGetData(sqlstatementhandle, 1, SQL_C_ULONG, &amp;id, 0, NULL);
              SQLGetData(sqlstatementhandle, 2, SQL_C_CHAR, name, 64, NULL);
              SQLGetData(sqlstatementhandle, 3, SQL_C_CHAR, address, 64, NULL);
              cout&lt;&lt;id&lt;&lt;" "&lt;&lt;name&lt;&lt;" "&lt;&lt;address&lt;&lt;endl;
          }
      }
  
  FINISHED:
      SQLFreeHandle(SQL_HANDLE_STMT, sqlstatementhandle );
      SQLDisconnect(sqlconnectionhandle);
      SQLFreeHandle(SQL_HANDLE_DBC, sqlconnectionhandle);
      SQLFreeHandle(SQL_HANDLE_ENV, sqlenvhandle);
  
  }
  </code></pre>
  
  <p>The problem is that I get MANY problems when trying to compile, regarding undeclared types, for example:</p>
  
  <pre><code>'SQLHANDLE' has not been declared
  'SQLHDESC' was not declared in this scope
  </code></pre>
  
  <p>I've linked to the <code>libodbccpp32.a</code> and <code>libodbc32.a</code> libraries already. What is that I'm doing wrong?</p>