title: Cannot retrieve data using C++ and SQL
body: <p>I'm successfully connecting to a SQL server database using a dsn-less connection.  Unfortunately, when I query my database it is not binding values to my id, species and description variables (i.e. even though, you'll see in my code that is exactly what I'm trying to do).</p>
  
  <p>The table I am trying to select from is listed below. For some reason, when I execute the query using <code>SQLExecDirect</code>, it either does not execute the statement or bind the values to id, species and description like I want it to (on the following lines).  Instead I get the ID, Species, Description column names (i.e. because it was just a cout), but no data underneath the column names (i.e. because the data is not being bound correctly).</p>
  
  <pre><code>MushroomID  Species                 Descript                Season
  
  1           Matsutake               Matsutake               Fall
  2           King Boletus            King Boletus            Fall
  3           Scaber Stalk Bolete     Scaber Stalk Bolete     Fall
  4           Chicken of the Woods    Chicken of the Woods    Fall
  5           Chanterelle             Chanterelle             Fall
  6           Lobster                 Lobster                 Fall
  </code></pre>
  
  <p>I have stepped through this multiple times, but have no idea what I'm looking at.  I'm a complete noob to SQL and using C++ is a nightmare to interact with SQL.  This is as far as I got, however.  </p>
  
  <p>The while statement in the code never runs.  hstmt is 0x0000 for some reason and I have no idea why.  The complete code is listed below. </p>
  
  <pre><code>#include &lt;sql.h&gt;
  #include &lt;sqlext.h&gt;
  #include &lt;iostream&gt;
  #include &lt;cstring&gt;
  using namespace std;
  
  void main()
  {
      SQLHANDLE henv;
      SQLHANDLE hdbc;
      SQLHANDLE hstmt;
      SDWORD cbQual;
  
      char * query = "USE MyName GO SELECT * FROM MyName.Mushroom;";
      char id[100], species[100], description[100];
  
      SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &amp;henv);
      SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, SQL_IS_INTEGER);
      SQLAllocHandle(SQL_HANDLE_DBC, henv, &amp;hdbc);
      SQLConnect(hdbc, (unsigned char *)"MyServer.brown.edu", SQL_NTS, 
                   (unsigned char *)"MyName", SQL_NTS,
                   (unsigned char *)"MyName", SQL_NTS);
      SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &amp;hstmt);
      SQLExecDirect(hstmt, (unsigned char *)query, SQL_NTS);
      SQLBindCol(hstmt, 1, SQL_CHAR, id, 100, &amp;cbQual);
      SQLBindCol(hstmt, 2, SQL_CHAR, species, 100, &amp;cbQual);
      SQLBindCol(hstmt, 3, SQL_CHAR, description, 100, &amp;cbQual);
      //SQLGetData (hstmt, 1, SQL_CHAR, id, 100, &amp;cbQual);
  
      cout &lt;&lt; "ID\tspecies\tDescription" &lt;&lt; endl
          &lt;&lt; "___\t________\t__________" &lt;&lt; endl;
      while(!SQLFetch(hstmt))
          cout &lt;&lt; id &lt;&lt; '\t' &lt;&lt; species &lt;&lt; "\t\t" &lt;&lt; description &lt;&lt; '\t' &lt;&lt; endl;
  
      SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
      SQLDisconnect(hdbc);
      SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
      SQLFreeHandle(SQL_NULL_HANDLE, henv);
  }
  </code></pre>
  
  <p>Can anyone spot any obvious errors that might be causing me htsmt to be 0x00000000 and id, species and description to contain nothing but garbage?</p>