News:

Accounts with zero posts and zero activity during the last months will be deleted periodically to fight SPAM!

Main Menu

C++ SQL Server Native Client ODBC

Started by SelfTeachingC++, October 08, 2019, 05:43:15 PM

Previous topic - Next topic

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:

#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,
sqlncli.h
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.



SelfTeachingC++

#2
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....

SelfTeachingC++

I'm just trying to learn, man.  No problem. Ill go somewhere else.

Bob

Thanks, SelfTeachingC++, for posting this -- and especially for sharing the solution.  I see your post has been read almost 300 times in a little over a month since you placed it.  We're not alone.  ;D

SelfTeachingC++

Man, I'm the type of person that only learns by actually doing something and learning the reasons why something is the way it is.  So even when I've learned something, I'm still researching it more to learn it better. Im never looking for a quick fix or, "the answer". I'm on a couple other forums, a few on SE, joined this one to be able to ask questions. I think these forums get inundated with so many people that just really have not done any research on their own that inevitably anything that can be smashed down is smashed down even if the question can be answered. "It's not the IDE, learn your compiler.", "It's not your compiler, learn programming.", "It's not programming, go learn your compiler." These types of responses infuriate me because if I'm here, I've done plenty of research and am looking for something else. A different source. A different look. One place that I have found where people will at least engage and try to help is on r/cpp_questions. Next time you're looking for help and you're getting nowhere here or on Stack Exchange, try there. They've helped me several times find what I'm looking for.

stahta01

C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]

SelfTeachingC++

Rule #1, if you can't find a suitable resource or still can't figure it out, ask r/cpp_questions.