ODBC API

Connecting to Data Sources

SQLAllocEnv

SQLAllocConnect

SQLConnect

SQLDriverConnect
(Level 1 Extension)

SQLBrowseConnect
(Level 2 Extension)

Disconnecting from a Data Source

SQLDisconnect

SQLFreeConnect

SQLFreeEnv

Setting & Retrieving Connection Options

SQLSetConnectOption
(Level 1 Extension)

SQLGetConnectOption
(Level 1 Extension)

SQLSetStmtOption
(Level 1 Extension)

SQLGetStmtOption
(Level 1 Extension)

Obtaining Information about a Driver or Data Source

SQLGetFunctions
(Level 1 Extension)

SQLGetTypeInfo
(Level 1 Extension)

SQLGetInfo
(Level 1 Extension)

SQLDataSources
(Level 2 Extension)

SQLDrivers
(Level 2 Extension)

Preparing SQL Requests to be Executed Multiple Times

SQLAllocStmt

SQLPrepare

SQLBindParameter
(Level 1 Extension)

SQLParamOptions
(Level 2 Extension)

SQLGetCursorName

SQLSetCursorName

SQLSetScrollOptions
(Level 2 Extension)

Submitting SQL Requests

SQLExecute

SQLExecDirect

SQLNativeSQL
(Level 2 Extension)

SQLDescribeParam
(Level 2 Extension)

SQLNumParams
(Level 2 Extension)

SQLParamData
(Level 1 Extension)

SQLPutData
(Level 1 Extension)

Retrieving Results and Information about Results

SQLRowCount

SQLNumResultCols

SQLDescribeCol

SQLColAttributes

SQLBindCol

SQLFetch

SQLExtendedFetch
(Level 2 Extension)

SQLGetData
(Level 1 Extension)

SQLSetPos
(Level 2 Extension)

SQLMoreResults
(Level 2 Extension)

SQLError

Terminating a Statement

SQLFreeStmt

SQLCancel

SQLTransact

Obtaining information about the Data Source's system tables (catalog functions)

SQLColumnPrivileges
(Level 2 Extension)

SQLColumns
(Level 1 Extension)

SQLForeignKeys
(Level 2 Extension)

SQLPrimaryKeys
(Level 2 Extension)

SQLProcedureColumns
(Level 2 Extension)

SQLProcedures
(Level 2 Extension)

SQLSpecialColumns
(Level 1 Extension)

SQLStatistics
(Level 1 Extension)

SQLTablePrivileges
(Level 2 Extension)

SQLTables
(Level 1 Extension)

ODBC C API DEFINITION: SQLConnect


Core

SQLConnect loads a driver and establishes a connection to a data source. The connection handle references storage of all information about the connection, including status, transaction state and error information.

SyntaxRETCODE SQLConnect(hdbc, szDSN, cbDSN, szUID, cbUID, szAuthStr, cbAuthStr)

The SQLConnect function accepts the following arguments.

Type

Argument

Use

Description

HDBChdbcInputConnection handle.
UCHAR FAR *szDSNInputData source name.
SWORDcbDSNInputLength of szDSN.
UCHAR FAR *szUIDInputUser identifier.
SWORDcbUIDInputLength of szUID.
UCHAR FAR *szAuthStrInputAuthentication string (typically the password).
SWORDcbAuthStrInputLength of szAuthStr.
ReturnsSQL_SUCCESS, SQL_SUCCESS_WITH_INFO, SQL_ERROR or SQL_INVALID_HANDLE
Diagnostics

When SQLConnect returns SQL_ERROR or SQL_SUCCESS_WITH_INFO, an associated SQLSTATE value may be obtained by calling SQLError. The following table lists the SQLSTATE values commonly returned by SQLConnect and explains each one in the context of this function; the notation "(DM)" precedes the descriptions of SQLSTATEs returned by the Driver Manager. The return code associated with each SQLSTATE value is SQL_ERROR, unless noted otherwise.

SQLSTATE

Error

Description

01000General warningDriver-specific informational message. (Function returns SQL_SUCCESS_WITH_INFO.)
08001Unable to connect to data sourceThe driver was unable to establish a connection with the data source.
08002Connection in use(DM) The specified hdbc had already been used to establisha connection with a data source and the connection was still open.
08004Data source rejected establishment of connectionThe data source rejected the establishement of the connection for implementation-defined reasons.
08S01Communication link failureThe communication link between the driver and the data source to which the drtiver was attempting to connect failed before the function completed processing.
28000Invalid authorization specificationThe value specified for the argument szUID or the value specified for the argument szAuthStr violated restrictions defined by the data source.
IM001Driver does not support this function(DM) The driver specified by the data source name does not support the function.
IM002Data source not found and no default driver specified(DM) The data source name specified in the argument SzDSN was not found in the ODBC.INI file or registry, nor was there a default driver specification.
IM003Specifided driver could not be loaded(DM) The driver listed in the data source specification in the ODBC.INI file or registration was not found or could not be loaded for some other reason.
IM005Driver's SQLAllocEnv failed(DM) During SQLConnect, the Driver Manager called the driver's SQLAllocEnv function and the driver returned an error.
IM006Driver's SQLSetConnectOption failed(DM) During SQLConnect, the Driver Manager called the driver's SQLSetConnectOption function and the driver returned an error. (Function returns SQL_SUCCESS_WITH_INFO.)
IM009Unable to load transaltion DLLThe driver was unable to load the translation DLL that was specified for the data source.
S1000General errorAn error occurred for which there was no specific SQLSTATE and for which no implementation-specific SQLSTATE was defined. The error message returned by SQLError in the argument szErrorMsg describes the error and its cause.
S1001Memory allocation failure(DM) The Driver Manager was unable to allocate memory for the connection handle. The driver was unable to allocate memory for the connection handle.
S1090

Invalid string or buffer length

(DM) The value specified for argument cbDSN exceeded the maximum length for a data source name.

(DM) The value specified for argument cbDSN exceeded the maximum length for a data source name.

(DM) The value specified for argument cbUID was less than 0, but not equal to SQL_NTS.

(DM) The value specified for argument cbAuthStr was less than 0, but not equal to SQL_NTS.

S1T00Timeout expiredThe timeout period expired before the connection to the data source completed. The timeout period is set through SQLSetConnectOption, SQL_LOGIN_TIMEOUT
Comments

The Driver Manager does not load a driver until the application calls a function (SQLConnect,SQLDriverConnect or SQLBrowseConnect) to connect to the driver. Until that point, the Driver Manager works with its own handles and manages connection information. When the application calls a connection function, the Driver Manager checks if a driver is currently loaded for the specified hdbc:

  • If a driver is not loaded, the Driver Manager loads the driver and calls SQLAllocEnv, SQLAllocConnect, SQLSetConnectOption if the application specified anyc onnection options), and the connection function in the driver. The Driver Manager returns SQLSTATE IM006 (Driver's SQLSetConnectOption failed) and SQL_SUCCESS_WITH_INFO for the connection function if the driver returned an error for SQLSetConnectOption.
  • If the specified driver is already loaded on the hdbc, the Driver Manager only calls the connection function in the driver. In this case, the driver must ensure that all connection options for the hdbc maintain their current settings.
  • If a different driver is loaded, the Driver Manager calls SQLFreeConnect and SQLFreeEnv in the loaded driver and then unloads that driver. It then performs the same operations as when a driver is not loaded.
The driver then allocates ahndles and initializes itself.

Note To resolve the address of the ODBC functions exported byt he driver, the Driver Manager checks if the driver exports a dummy function with the ordinal 199. If it does not, the Driver Manager resolves the addresses by name. If it does, the Driver Manager resolves the addresses of the ODBC functions by ordinal, which is faster. The ordinal values of the ODBC functions must match the values of the fFunction argument in SQLGetFunctions; all other exported functions (such as WEP) must have ordinal values outside the range 1-199.


When the application calls SQLDisconnect, the Driver Manager calls SQLDisconnect in the driver. However, it does not unload the driver. This keeps the driver in memory for applications that repeatedly connect to and disconnect from a data source. WHen the application calls SQLFreeConnect, the Driver Manager calls SQLFreeConnect and SQLFreeEnv in the driver and then unloads the driver.

An ODBC application can establish more than one connection.

Driver Manager Guidelines

The contents of a szDSN affect how the Driver Manager and a driver work together to establish a connection to a data source.

  • If szDSN contains a valid data source name, the Driver Manager locates the corresponding data source specification in the ODBC.INI file or registry and loads the associated driver DLL. The Driver Manager passes each SQLConnect argument to the driver.
  • If the data source name cannot be found or szDSN is a null pointer, the Driver Manager locates the default data source specification and loads the associated driver DLL. The Driver Manager passes each SQLConnect argument to the driver.
  • If the data source name cannot be found or szDSN is a null pointer, and the default data source specification does not exist, the Driver Manager returns SQL_ERROR with SQLSTATE IM002 (Data source name not found and no default driver specified).

After being loaded by the Driver Manager, a driver can locate its corresponding data source specification in the ODBC.INI file or registry and use driver-specific information from the specification to complete its set of required connection information.

If a default translation DLL is specified in the ODBC.INI file or registraty for the data source, the driver loads it. A different translation DLL can be loaded by calling SQLSetConnectOption with the SQL_TRANSLATE_DLL option. A translation option can be specified by calling SQLSetConnectOption with the SQL_TRANSLATION_OPTION option.

Code Example(future)
Related Functions
For information about

See

Allocating a connection handleSQLAllocConnect
Allocating a statement handleSQLAllocStmt
Discovering and enumerating values required to connect to a data sourceSQLBrowseConnect (extension)
Disconnecting from a data sourceSQLDisconnect
Connecting to a data source using a connection string or dialog box.SQLDriverConnect (extension)
Returning the setting of a connection optionGetConnectOption (extension)
Setting a connection optionSetConnectOption (extension)

ODBC Router

ODBC Router transparently makes all ODBC drivers on a central Windows Server useable by your network's iPhone/iPod, iPad, Linux, Mac and Windows systems. Years in the making here in the USA, ODBC Router has saved its customers millions of dollars in DLL installations and support costs for less than the price of a new PC and a few minutes installation time. ODBC Router provides a low cost, turnkey database network with enterprise class IT support.

Ditching the ODBC Administrator Control Panel:

By linking your application with the free ODBC Router client-side SDK instead of the ODBC Driver Manager, the need for your end-users to deal with driver installation or an ODBC Control Panel is gone! We even provide a setup function your code may call to display a "network browser" that enables your customer to "find" their ODBC Router server on the network, then "choose" the exact database they want to work with and finally return to your application with a fully-formed ODBC connection-string that may be stored and passed back to SQLConnect or SQLDriverConnect anytime your user wants to initiate a connection that data source. No more walking your customers through the process of adding data sources to ODBC Control Panel or, in the case of non-Windows computers, no need to worry about whether or not a compatible third-party ODBC Driver Manager has been installed for use with your code. (Remember that ODBC Router supports iPhone/iPod, iPad, Mac OS 9, Mac OS X, Linux and Windows.) If there ever are any client-side ODBC support issues, AugSoft can handle them directly with the customer freeing you to focus on the application.

JDBC?

JDBC-in-ODBC drivers (like some 'actual' ODBC drivers, lol) launch a CPU-intensive virtual machine in the background on your machine, which is bad for battery powered laptops, high-volume web servers or entry level desktops (that typically have slow busses and drives). As the world shifted to laptops and shared servers, the whole "virtual machine" concept became a support nightmare and so these days good Java apps are compiled to run as native (not emulated) code. Java developers may use the operating system's native ODBC support from within the JDBC class library using the sun.jdbc.odbc.JdbcOdbcDriver driver with a URL as shown below.

jdbc:odbc:dsn[;key=value]*

Example:

jdbc:odbc:finance;UID=cfo;

IT techs may then complete the database connection on the Customer's machine using ODBC Router or the database vendor's official ODBC driver.

NOTE: By creating ODBC data sources with ODBC Router, your apps will enjoy native speed and database independent connections from either Java/C/C++/C#/ObjC or PHP/PERL/Python/Ruby/BASIC on Linux, Macintosh and Windows. Also be aware that using ODBC Router with the Mac platform is an especially good idea because database vendors have not kept their Mac drivers in sync with Windows and there are actual third-party vendors who wrap freeware and JDBC drivers inside of ODBC "shells" without warning their customers! This problem is of great concern to developers because fake drivers almost always fly past the IT guys who test with speed deamon desktops, but fail the enterprise when user laptops and iMacs take too long to run queries or slowly corrupt the database when they do. IT guys often chalk this up to "network problems" leaving users with poison drivers to avoid their database. ODBC Router addresses this issue by enabling official vendor supported Windows ODBC drivers (on a Windows Server) to be accessed from all platforms, network wide.

ODBC 3.x?

It's not here yet. Even in 2010, most ODBC drivers are ODBC 1.x and 2.x. The ODBC Driver Manager translates between 3.x and 2.x or 2.x and 1.x ODBC calls. Therefore, if you don't need UNICODE, it's a bad idea to use ODBC 3.x API calls. That said, UNICODE is a Good Thing and there are actually at least three databases that natively support it now, so look for 3.x to be here soon.

Need ODBC API Help?

We really know ODBC and we routinely provide code-level ODBC help to our customers. Our low cost ODBC Router systems are available now at our online store and if your site buys them, you may open a ticket to ask ODBC development questions --we offer both E-Mail and On-Call support options in seven of the G8's timezones! Be sure to test your ODBC Router and ask for any needed installation help before purchase because we aren't Fry's --no refunds please, our prices are too low for such nonsense and we're too busy supporting real Customers!

© Copyright 1990-2010, August Software, USA. All Rights Reserved.