Connectivity Guide > Connectivity Guide > Understanding ODBC Connectivity > ODBC Programming > SQLDriverConnect()--Connect without Using a Data Source Name
Was this helpful?
SQLDriverConnect()--Connect without Using a Data Source Name
You can bypass ODBC DSN information completely if you use the SQLDriverConnect() function instead of SQLConnect(). SQLDriverConnect() uses a connection string instead of an ODBC DSN definition; the odbc.ini file is not used unless the DSN connection string attribute is specified in the connection string.
Connection strings are typically used in higher-level languages that use ODBC, such as:
Microsoft ADO
Microsoft OLE DB Provider for ODBC
Microsoft .NET Data Provider for ODBC
PHP ODBC interface
Python DBI driver
JDBC/ODBC Bridge
An ODBC connection string is structured as a set of “attribute, value” pairs in the following abstract:
connectStr = attribute=value[;attribute=value[;...]]
Each set of “attribute, value” pairs is separated by a semicolon (“;”). No spaces or other separators, such as <TAB>, are allowed between semicolons and the next attribute pair.
The attribute pair “DSN=myDSN” causes SQLDriverConnect() to search for an ODBC DSN specification just as in SQLConnect(). However, other connection attributes specified in the connection string override any similar specification in the ODBC DSN definition.
SQLDriverConnect() requires you to provide the following minimum connection attributes:
Database
Type of driver
Type of database (Actian X, SQLServer, Oracle, etc.)
Server. The server is the name of a vnode (server connection) definition in Director, netutil, ingnet, or VDBA. If you are connecting locally, the default string name “(local)” indicates a local connection.
The following program using SQLDriverConnect() performs exactly the same function as the previous code example.
Example: SQLDriverConnect() Function
# ifdef _WIN32
# include <windows.h>
# else
# include <stdio.h>
# include <stdlib.h>
# endif
# include <sql.h>
# include <sqlext.h>
 
main()
{
    HENV henv=NULL;
    HDBC hdbc=NULL;
    SQLCHAR connectString[256];
 
    /* Initialize the ODBC environment handle. */
    SQLAllocHandle( SQL_HANDLE_ENV, NULL, &henv );
 
    /* Set the ODBC version to version 3 (the highest version) */
    SQLSetEnvAttr( henv, SQL_ATTR_ODBC_VERSION,
        (void *)SQL_OV_ODBC3, 0 );
 
    /* Allocate the connection handle. */
    SQLAllocHandle( SQL_HANDLE_DBC, henv, &hdbc );
 
    /*
    ** Fill the connection string with the minimum
    ** connection information.
    */
    strcpy( connectString, "driver={Ingres};servertype=Ingres;" \
        "server=(local);database=myDB" );
 
    /* Connect to the database using the connection string. */
    SQLDriverConnect( hdbc,    /* Connection handle */
        0,                     /* Window handle */
        connectString,         /* Connection string */
        SQL_NTS,               /* This is a null-terminated string */
        (SQLCHAR *)NULL,       /* Output (result) connection string */
        SQL_NTS,               /* This is a null-terminated string */
        0,                     /* Length of output connect string */
        SQL_DRIVER_NOPROMPT ); /* Don't display a prompt window */
 
    /* Disconnect from the database. */
    SQLDisconnect( hdbc );
 
    /* Free the connection handle. */
    SQLFreeHandle( SQL_HANDLE_DBC, hdbc );
 
    /* Free the environment handle. */
    SQLFreeHandle( SQL_HANDLE_ENV, henv );
 
    /* Exit this program. */
    return(0);
}
The previous example applies to Windows programs. On Linux, if you are using the ODBC CLI, only the database name is required as the minimum connection attribute:
strcpy( connectString, "database=myDB" );
The ODBC CLI assumes that the driver type is “Ingres”, the server type is “Ingres”, and that the server is “(local)” if you do not so specify.
A list of ODBC connection attributes and values is available in Connection String Keywords.
Last modified date: 08/14/2024