User forums > Using Code::Blocks

C++ SQL Server Native Client ODBC

(1/2) > >>

SelfTeachingC++:
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:


--- Code: ---#include <windows.h>
#include <sqltypes.h>
#include <sql.h>
#include <odbcinst.h>
--- End code ---

yet I get undefined reference errors for the SQL Server native ODBC function calls


--- Code: ---SQLAllocHandle
SQLSetEnvAttr
SQLDriverConnect
--- End code ---

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,

--- Code: ---sqlncli.h
--- End code ---

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,


--- Code: ---#include <windows.h>
#include <sqltypes.h>
#include <sql.h>
#include <odbcinst.h>
--- End code ---

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:


--- Code: ---odbc32.lib
odbcbcp.lib
odbccp32.lib
--- End code ---

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.

BlueHazzard:
http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28general%29#Q:_How_do_I_report_a_compilation_problem_on_the_forums.3F

SelfTeachingC++:
Ok thanks...wasn't really looking for a solution to my specific problem but somewhere that might teach me a little more about doing this or have a tutorial on it.

BlueHazzard:
We do not support "gerenal" questions in this forum. Learn how to use the library, search on youtube or internet.
We do support questions like "i have this make file and would like to use it in codeblocks" or "i have this .a file, where do i put it?"  or "i need to add this compiler flag, where do i add it?" or also some kind of compiler problems when they are related to codeblocks.

BlueHazzard:
And you have some compiler error that we can try to fix, but you have to provide the build log like described in my link. Without this build log we are not able to help you....

Navigation

[0] Message Index

[#] Next page

Go to full version