Connect to Salesforce using C/C++

Last modified: July 25, 2025

You can connect to Salesforce from C/C++ using Devart ODBC Driver for Salesforce.

Define a connection string

An ODBC connection requires a connection string, which can either use a predefined DSN or be specified through connection string parameters (a DSN-less connection).

For information about available parameters, see Connection string parameters.

DSN connection string

You can use a connection string with a predefined DSN.

SQLWCHAR connectionString[] = "DSN=your_dsn";

If you need credentials other than those used in the DSN, specify them in the connection string to override the DSN values.

SQLWCHAR connectionString[] = "DSN=your_dsn;User ID=your_username;Password=your_password";

For information on configuring a DSN on specific operating systems, see instructions for Windows, macOS, or Linux.

DSN-less connection string

You can establish a connection without a DSN by specifying all necessary parameters directly in the connection string.

The following example uses OAuth 2.0 authentication.

SQLWCHAR connectionString[] = "DRIVER=Devart ODBC Driver for Salesforce;Authentication=OAuth;Refresh Token=your_refresh_token";

For more information, see DSN-less connections.

Connect to Salesforce

The following C/C++ code demonstrates how to connect to Salesforce and fetch data using the ODBC driver.

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <tchar.h>
#include <sql.h>
#include <sqlext.h>

int _tmain(int argc, _TCHAR* argv[]) {
    SQLHENV hEnv = NULL;
    SQLHDBC hDbc = NULL;
    SQLHSTMT hStmt = NULL;
    SQLRETURN ret;

    // Define an ODBC connection string
    SQLWCHAR connectionString[] = L"your_connection_string";

    // Allocate an environment handle
    ret = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv);
    if (ret == SQL_ERROR) {
        wprintf(L"Unable to allocate an environment handle\n");
        exit(-1);
    }

    // Set the ODBC version environment attribute
    SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);

    // Allocate a connection handle
    ret = SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hDbc);
    if (ret == SQL_ERROR) {
        wprintf(L"Unable to allocate a connection handle\n");
        goto Exit;
    }

    // Connect to the data source
    ret = SQLDriverConnect(hDbc, 
                           NULL, 
                           connectionString, 
                           SQL_NTS, 
                           NULL, 
                           0, 
                           NULL, 
                           SQL_DRIVER_NOPROMPT);
    if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO) {
        wprintf(L"Connection was successfully established!\n");
    } else {
        wprintf(L"Failed to open the connection.\n");
        goto Exit;
    }

    // Allocate a statement handle
    ret = SQLAllocHandle(SQL_HANDLE_STMT, hDbc, &hStmt);
    if (ret == SQL_SUCCESS) {
        // Define your SQL query
        SQLWCHAR query[] = L"SELECT * FROM Account";

        // Execute the SQL query
        ret = SQLExecDirect(hStmt, query, SQL_NTS);
        if (ret == SQL_SUCCESS) {
            SQLWCHAR colData[256];
            SQLLEN colDataLen;

            // Fetch and display the results
            while (SQLFetch(hStmt) == SQL_SUCCESS) {
                SQLGetData(hStmt, 1, SQL_C_WCHAR, colData, sizeof(colData), &colDataLen);
                wprintf(L"Account Name: %s\n", colData);
            }
        } else {
            wprintf(L"Failed to execute the query.\n");
            goto Exit;
        }
    }

Exit:
    // Free the statement handle
    if (hStmt) {
        SQLFreeHandle(SQL_HANDLE_STMT, hStmt);
    }

    if (hDbc) {
        // Close the connection
        SQLDisconnect(hDbc);
        wprintf(L"Connection was closed.\n");
        // Free the connection handle
        SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
    }

    // Free the environment handle
    if (hEnv) {
        SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
    }

    return 0;
}

Request password or refresh token in the connection dialog

If the password or refresh token isn’t specified in the connection string, pass SQL_DRIVER_COMPLETE or SQL_DRIVER_COMPLETE_REQUIRED as the argument to the DriverCompletion parameter to prompt the user for this information through the connection dialog.

SQLDriverConnect(hDbc, 
                 GetDesktopWindow(), 
                 connectionString, 
                 SQL_NTS, 
                 NULL, 
                 0, 
                 NULL, 
                 SQL_DRIVER_COMPLETE);

To display the connection dialog regardless of whether password or refresh token is specified, pass SQL_DRIVER_PROMPT to the DriverCompletion parameter.