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

Offline SelfTeachingC++

  • Single posting newcomer
  • *
  • Posts: 6
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: 6
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: 2590
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: 2590
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: 6
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

Offline SelfTeachingC++

  • Single posting newcomer
  • *
  • Posts: 6
Re: C++ SQL Server Native Client ODBC
« Reply #7 on: January 26, 2020, 01:54:43 am »
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.

Online stahta01

  • Lives here!
  • ****
  • Posts: 6768
    • My Best Post
Re: C++ SQL Server Native Client ODBC
« Reply #8 on: January 26, 2020, 02:32:11 am »
This is a Hyper link follow it and then follow the rules!!!!!
http://forums.codeblocks.org/index.php/topic,9996.0.html
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline SelfTeachingC++

  • Single posting newcomer
  • *
  • Posts: 6
Re: C++ SQL Server Native Client ODBC
« Reply #9 on: January 26, 2020, 07:54:47 pm »
Rule #1, if you canít find a suitable resource or still canít figure it out, ask r/cpp_questions.