How to Access CData ODBC Drivers Through C++ Code

  • 16 November 2023
  • 0 replies
  • 4 views

Userlevel 1
Badge

This article guides you through seamlessly integrating CData ODBC drivers into your C++ project, utilizing Visual Studio 2022. We'll use the CData ODBC driver for JSON as an example, but keep in mind that you can apply the same principles to any other CData ODBC driver of your choice. 

 

 

1. Downloading and Installing CData ODBC Driver for JSON 

  • Start by obtaining the CData ODBC driver for JSON. Visit CData's download page and run the downloaded setup file to install the driver. 

https://www.cdata.com/drivers/json/download/odbc/  

 

 

2. Configuring the DSN (Data Source Name) 

  • During installation, ensure to select the "Configure ODBC Data Source" option in the final view. If you've already installed the driver, locate 'ODBC Data Sources' in the Windows search bar. Choose your desired DSN and click 'Configure'. 

 

 

3. Adding the URI Connection Property 

  • Next, integrate the path to your JSON file in the URI connection property. For example, if you're using a JSON file containing a list of cities and related data, input its path. Afterward, click 'Test Connection' and then 'OK' twice. 

 

 

4. Setting Up Visual Studio 2022 

  • If not already installed, obtain Visual Studio 2022. During installation, on the 'Workloads' page, make sure to include 'Desktop development with C++'. Additionally, within this workload, include the Windows SDK. 

  • If you've already installed Visual Studio, launch the Visual Studio Installer and add the necessary packages. 

 

 

5. Creating a New Project in Visual Studio 

  • Open Visual Studio and on the 'Create a new project' window, select 'an empty C++ project. Proceed by clicking 'Next' and then 'Create'. Ensure that Windows SDK is included in your project by right clicking on your project on Solutions Explorer and clicking on Properties. On General Properties you can see your Windows SDK Version. 

6. Utilizing the ODBC Driver in Your Project 

  • Within your project, create a new C++ file (with a .cpp extension) and use the provided code as a starting point. 

This is the example of the code that calls the DSN and executes a query on it. 

#include <stdio.h> 

#include <stdlib.h> 

#include <windows.h> 

#include <Odbcinst.h> 

#include <Sql.h> 

#include <Sqlext.h> 

#include <Sqltypes.h> 

#include <Sqlucode.h> 

 

 

 

int main() 

    SQLHENV henv; 

    SQLHDBC hdbc; 

    SQLHSTMT hstmt; 

    char sId[255] = { 0 }; 

    SQLLEN cbId = 0; 

    SQLWCHAR name[] = L"CData JSON Source"; 

    SQLWCHAR query[] = L"SELECT * FROM cities"; 

    if (SQLAllocHandle(SQL_HANDLE_ENV, 0, &henv) == SQL_SUCCESS) { 

        SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0); 

        if (SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc) == SQL_SUCCESS) { 

            if (SQLConnect(hdbc, name, SQL_NTS, NULL, SQL_NTS, NULL, SQL_NTS) == SQL_SUCCESS) { 

                if (SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt) == SQL_SUCCESS) { 

                    if (SQLExecDirect(hstmt, query, SQL_NTS) == SQL_SUCCESS) { 

                        while (SQLFetch(hstmt) == SQL_SUCCESS) { 

                            if (SQLGetData(hstmt, 1, SQL_C_CHAR, (SQLPOINTER)sId, sizeof(sId), &cbId) == SQL_SUCCESS) { 

                                printf("Name: %s\n", sId); 

                            } 

                        } 

                    } 

                    SQLFreeHandle(SQL_HANDLE_STMT, hstmt); 

                } 

                SQLDisconnect(hdbc); 

            } 

            SQLFreeHandle(SQL_HANDLE_DBC, hdbc); 

        } 

    } 

 

In the next article we are going to delve deeper on how to customize this code according to various situations. 

 

Conclusion: 

By integrating CData ODBC drivers with your C++ project, you've unlocked a powerful tool for seamless data retrieval. This process, demonstrated using Visual Studio 2022, allows you to efficiently work with various data sources, enhancing the capabilities of your application. 

Remember, the steps outlined here serve as a foundation. Depending on your specific project and data source, further customization may be necessary. With the right ODBC driver and a solid understanding of integration, you're well-equipped to tackle data-driven challenges in your C++ projects. 

Happy coding! 


This topic has been closed for comments