MS VC Looking for working C++ using ODBC calls.

Trying to create code to open an ODBC DB on my Windows 10 PC using MS Visual Studio 2019. Have found a bunch of sample code that is suppose to open an ODBC connection to a DB and run a simple query. But none of the samples I've found will compile without errors. Some are ten years old, so I expect things have changed, but something two years old should be working, right?

If you know of some sample code that is recent, please let me know.

I was very surprised that the sample code on Microsoft's web site also gave a lot of compiler errors.

Thanks,
Tim.
http://www.catb.org/esr/faqs/smart-questions.html#beprecise
In other words,
Actual links to code you've tried.
Actual code you've tried.
Actual error messages.

> If you know of some sample code that is recent, please let me know.
How would we know we weren't repeating what you've already seen?
Hmmmm. Well, try these two programs for a start and let me know what happens. I named them Test01.cpp and Test02.cpp....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// cl Test01.cpp ODBC32.lib
#include <windows.h>
#include <stdio.h>
#include <sqlext.h>

int main()
{
 SQLRETURN iReturn = 0;
 SQLHENV   hEnvr   = NULL;;

 iReturn=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&hEnvr);
 if(iReturn==SQL_SUCCESS)
 {
    printf("SQLAllocHandle(hEnvr) Succeeded!\n");
    SQLFreeHandle(SQL_HANDLE_ENV,hEnvr);
 }
 else
    printf("SQLAllocHandle(hEnvr) Failed!\n");
 getchar();

 return 0;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// cl Test02.cpp odbc32.lib
#include <windows.h>
#include <stdio.h>
#include <sqlext.h>

int main()
{
 SQLRETURN iReturn = 0;
 SQLHENV   hEnvr   = NULL;;
 
 iReturn=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&hEnvr);
 if(iReturn==SQL_SUCCESS)
 {
    printf("SQLAllocHandle(hEnvr) Succeeded!\n");
    iReturn=SQLSetEnvAttr(hEnvr,SQL_ATTR_ODBC_VERSION,(SQLPOINTER)SQL_OV_ODBC3,SQL_IS_INTEGER);
    if(iReturn==SQL_SUCCESS)
       printf("SQLSetEnvAttr(hEnvr) Succeeded!\n");
    else
       printf("SQLSetEnvAttr(hEnvr) Failed!\n");
    SQLFreeHandle(SQL_HANDLE_ENV,hEnvr);
 }
 else
    printf("SQLAllocHandle(hEnvr) Failed!\n");
 getchar();

 return 0;
}


Note that I only do command line compiling, and I have the build string listed at top for both. But if you are using the IDE you'll need to do whatever it takes to tall the thing to add ODBC32.lib to the link options.
Registered users can post here. Sign in or register to post.