Author Topic: C++ SQL Server Native Client ODBC  (Read 563 times)

Offline SelfTeachingC++

  • Single posting newcomer
  • *
  • Posts: 4
C++ SQL Server Native Client ODBC
« on: October 08, 2019, 05:43:15 pm »
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: [Select]
#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

Code: [Select]
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,
Code: [Select]
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,

Code: [Select]
#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:

Code: [Select]
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.

« Last Edit: October 09, 2019, 10:51:04 pm by SelfTeachingC++ »


Offline SelfTeachingC++

  • Single posting newcomer
  • *
  • Posts: 4
Re: C++ SQL Server Native Client ODBC
« Reply #2 on: October 08, 2019, 06:54:04 pm »
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.
« Last Edit: October 08, 2019, 07:17:59 pm by SelfTeachingC++ »

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2566
Re: C++ SQL Server Native Client ODBC
« Reply #3 on: October 08, 2019, 08:18:35 pm »
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.

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2566
Re: C++ SQL Server Native Client ODBC
« Reply #4 on: October 08, 2019, 08:19:31 pm »
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....

Offline SelfTeachingC++

  • Single posting newcomer
  • *
  • Posts: 4
Re: C++ SQL Server Native Client ODBC
« Reply #5 on: October 08, 2019, 08:54:56 pm »
I'm just trying to learn, man.  No problem. Ill go somewhere else.

Offline Bob

  • Single posting newcomer
  • *
  • Posts: 1
Re: C++ SQL Server Native Client ODBC
« Reply #6 on: November 11, 2019, 05:23:04 am »
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