I want to set up a simple connection to a SQL Server table to learn more about interfacing C++ and SQL Server. Anyone have experience doing this with C::B? Anyone know of any tutorials on this? I see tutorials involving the SQLAPI++ (I don't really want to use it yet). I'd prefer to try to create a connection with the native client ODBC driver.
I've included what I think are the appropriate headers as far as I can tell:
#include <windows.h>
#include <sqltypes.h>
#include <sql.h>
#include <odbcinst.h>
yet I get undefined reference errors for the SQL Server native ODBC function calls
SQLAllocHandle
SQLSetEnvAttr
SQLDriverConnect
I'm sure the others will generate the same errors, I just haven't tried using them yet. I see vague references to linker errors in regard to a file ODBC32.lib but a filename search turns up no such file in the compiler directories, SQL Server lib directories or entire computer for that matter.
I read on the microsoft docs site that I also had to include another header,
but that turned up a multitude of errors within itself (im sure this was in the way I was using it but it appeared the errors were multiple declarations of the same variable within sqlncli.h...No doubt, I'm misusing sqlncli.h and misinterpreting the errors).
Using Windows 10, C::B 17.12 and SQL Server 2008 Express
EDIT:
So I figured this out and since there are no places online that really explain it, I'll post in case anyone else is trying to figure it out.
Things like this would be easiest with Visual Studio since it's already set up with the libraries in the correct place but I'm using C::B.
The header files I mentioned above,
#include <windows.h>
#include <sqltypes.h>
#include <sql.h>
#include <odbcinst.h>
contain the declarations for the SQLAllocHandle, SQLSetEnvAttr and SQLDriverConnect functions of the Microsoft ODBC Drivers (and Im assuming other function declarations that I haven't gotten to yet). You need the following libraries for the function definitions:
odbc32.lib
odbcbcp.lib
odbccp32.lib
They can be made available for the linker under the settings menu for the compiler, on the linker settings and search directories tabs OR for the project specifically by right clicking the project and choosing Build Options and and then the linker settings and search directories tabs. You can get the libraries for your OS by downloading the Windows SDKs from the microsoft download page.