title: Sql express SQL driver connect C++
body: <p>Greeting guys , I have been looking on the internet on how to connect C++ code with SQLEXPRESS DATABASE, I have read some threads about how to connect but yet... I CAN NOT follow any of them... neither they are working for me.</p>
  
  <p>I dont want to use MFC or any kind of C++ methods for DB , I want to do it via raw coding.</p>
  
  <p>problem:</p>
  
  <ol>
  <li><p>the Database connectable via Visual studio and via Microsoft SQL
  Server Studio , I can add or drop tables as I LIKE, I believe the DB
  as it is, is working fine</p></li>
  <li><p>the C++ code is here " I am Connecting to a local DB "</p></li>
  <li><p>I got this code from one of the threads and was not able to figure out how to do the SQLDriverConnect, </p></li>
  <li><p>I am very noob in C++... adding SQLEXPRESS on top of that...</p></li>
  </ol>
  
  <p>edit:</p>
  
  <ol start="5">
  <li>I dont know what that 3055 in the connect code is...</li>
  </ol>
  
  <p>thank you in advance for help</p>
  
  <p>Data base information:</p>
  
  <p>UDL:
   this what I got from the udl  </p>
  
  <pre><code>( Provider=SQLOLEDB.1;Integrated Security=SSPI;
    Persist Security Info=False;User ID=sa;Initial Catalog=Holpa;Data Source=AMH )
  </code></pre>
  
  <p>Microsoft server:</p>
  
  <pre><code>Server type: Database Engine
  Server name: AMH
  Authen: SQL server Authen
  Login: sa 
  password : amh999
  </code></pre>
  
  <p>VisualStudio:</p>
  
  <pre><code>Data Source=AMH;Initial Catalog=Holpa;User ID=sa;Password=***********
  </code></pre>
  
  <p>.NET Framework Data Provider for SQL Server</p>
  
  <pre><code>Open
  Microsoft SQL Server
  Owner sa
  </code></pre>
  
  <p>running on local machine.</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;
  
  SQLHANDLE sqlenvhandle = SQL_NULL_HANDLE;
  SQLHANDLE sqlconnectionhandle = SQL_NULL_HANDLE;
  SQLHANDLE sqlstatementhandle = SQL_NULL_HANDLE;
  SQLRETURN retcode;
  void show_error(RETCODE rc, SQLHENV hEnv, SQLHDBC hDbc,
  SQLHSTMT hStmt, const char *action)
  {
      SQLWCHAR szMessage[256];
      SQLWCHAR szState[6];
      SDWORD sdwNative;
      SWORD swMsgLen;
  
      SQLError(hEnv, hDbc, hStmt, szState, &amp;sdwNative, szMessage,
      sizeof(szMessage), &amp;swMsgLen);
      wcout &lt;&lt; "wcout MESSAGE: " &lt;&lt; szMessage &lt;&lt; "\n SQLSTATE " &lt;&lt; szState &lt;&lt;      endl;
      printf("Error %d performing %s\n SQLState=%s\nSQL message=%s\n",rc, action, szState, szMessage);
  }
  
  void CloseSQL()
  {
      SQLFreeHandle(SQL_HANDLE_STMT, sqlstatementhandle);
      SQLDisconnect(sqlconnectionhandle);
      SQLFreeHandle(SQL_HANDLE_DBC, sqlconnectionhandle);
      SQLFreeHandle(SQL_HANDLE_ENV, sqlenvhandle);
  }
  
  
  int main()
  {
  
  if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &amp;sqlenvhandle))
  {
      printf("huh \n");
      CloseSQL();
      goto END;
  }
  
  if (SQL_SUCCESS != SQLSetEnvAttr(sqlenvhandle, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0))
  {
      printf("huh \n");
      CloseSQL();
      goto END;
  }
  
  if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_DBC, sqlenvhandle, &amp;sqlconnectionhandle))
  {
      printf("huh \n");
      CloseSQL();
      goto END;
  }
  printf("Driver Initialised\n");
  SQLWCHAR retconstring[1024];
  printf("about to Driver Conneect\n");
  retcode = SQLDriverConnect(sqlconnectionhandle,
      NULL,
      (SQLWCHAR*)"DRIVER={SQL Server};SERVER=AMH, 3055;DATABASE=Holpa;UID=sa;PWD=amh999;",
      SQL_NTS,
      retconstring,
      1024,
      NULL,
      SQL_DRIVER_NOPROMPT);
  
  
  if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
  {
   printf("Connection made\n");
  }
  else
  {
   show_error(retcode, sqlenvhandle, sqlconnectionhandle, sqlstatmenthandle, "Connecting.");
  }
  
  END:
      printf("\n");
      printf("Program End, press enter key to exit!");
      getchar(); // waits for input 
      return 0;
  }
  </code></pre>
  
  <p>ERRORS:
  the error codes are not constants... they keep changing ... example I got the following:
  Message: 0022E1EC
  SQLSTATE: 0022e9f4</p>
  
  <p>re-run the program
  Message: 009fdc4c
  SQLSTATE: 009fe454</p>
  
  <p>rerun the code
  Message: 00aadd90
  SQLSTATE: 00aae598</p>