Conformance
Version Introduced:ODBC 1.0
Standards Compliance:ODBC
Summary
SQLBrowseConnect supports an iterative method of discovering and enumerating the attributes and attribute values required to connect to a data source. Each call to SQLBrowseConnect returns successive levels of attributes and attribute values. When all levels have been enumerated, a connection to the data source is completed and a complete connection string is returned by SQLBrowseConnect. A return code of SQL_SUCCESS or SQL_SUCCESS_WITH_INFO indicates that all connection information has been specified and the application is now connected to the data source.
Syntax
SQLRETURN SQLBrowseConnect(
SQLHDBCConnectionHandle,
SQLCHAR *InConnectionString,
SQLSMALLINTStringLength1,
SQLCHAR *OutConnectionString,
SQLSMALLINTBufferLength,
SQLSMALLINT *StringLength2Ptr);
Arguments
ConnectionHandle
[Input]
Connection handle.
InConnectionString
[Input]
Browse request connection string (see “InConnectionString Argument” in “Comments”).
StringLength1
[Input]
Length of *InConnectionString.
OutConnectionString
[Output]
Pointer to a buffer in which to return the browse result connection string (see “OutConnectionString Argument” in “Comments”).
BufferLength
[Input]
Length of the *OutConnectionString buffer.
StringLength2Ptr
[Output]
The total number of bytes (excluding the null-termination byte) available to return in *OutConnectionString. If the number of bytes available to return is greater than or equal to BufferLength, the connection string in *OutConnectionString is truncated to BufferLength minus the length of a null-termination character.
Returns
SQL_SUCCESS, SQL_SUCCESS_WITH_INFO, SQL_NEED_DATA, SQL_ERROR, or SQL_INVALID_HANDLE.
Diagnostics
When SQLBrowseConnect returns SQL_ERROR, SQL_SUCCESS_WITH_INFO, or SQL_NEED_DATA, an associated SQLSTATE value may be obtained by calling SQLGetDiagRec with a HandleType of SQL_HANDLE_STMT and a Handle of ConnectionHandle. The following table lists the SQLSTATE values commonly returned by SQLBrowseConnect 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 |
01000 | General warning | Driver-specific informational message. (Function returns SQL_SUCCESS_WITH_INFO.) |
01004 | String data, right truncated | The buffer *OutConnectionString was not large enough to return entire browse result connection string, so the string was truncated. The buffer *StringLength2Ptr contains the length of the untruncated browse result connection string. (Function returns SQL_SUCCESS_WITH_INFO.) |
01S00 | Invalid connection string attribute | An invalid attribute keyword was specified in the browse request connection string (InConnectionString). (Function returns SQL_NEED_DATA.) An attribute keyword was specified in the browse request connection string (InConnectionString) that does not apply to the current connection level. (Function returns SQL_NEED_DATA.) |
01S02 | Value changed | The driver did not support the specified value of the ValuePtr argument in SQLSetConnectAttr and substituted a similar value. (Function returns SQL_SUCCESS_WITH_INFO.) |
08001 | Client unable to establish connection | The driver was unable to establish a connection with the data source. |
08002 | Connection name in use | (DM) The specified connection had already been used to establish a connection with a data source and the connection was open. |
08004 | Server rejected the connection | The data source rejected the establishment of the connection for implementation-defined reasons. |
08S01 | Communication link failure | The communication link between the driver and the data source to which the driver was attempting to connect failed before the function completed processing. |
28000 | Invalid authorization specification | Either the user identifier or the authorization string or both as specified in the browse request connection string (InConnectionString) violated restrictions defined by the data source. |
HY000 | General error | An error occurred for which there was no specific SQLSTATE and for which no implementation-specific SQLSTATE was defined. The error message returned by SQLGetDiagRec in the *MessageText buffer describes the error and its cause. |
HY001 | Memory allocation error | (DM) The Driver Manager was unable to allocate memory required to support execution or completion of the function. The driver was unable to allocate memory required to support execution or completion of the function. |
HY013 | Memory management error | The function call could not be processed because the underlying memory objects could not be accessed, possibly because of low memory conditions. |
HY090 | Invalid string or buffer length | (DM) The value specified for argument StringLength1 was less than 0 and was not equal to SQL_NTS. (DM) The value specified for argument BufferLength was less than 0. |
HYT00 | Timeout expired | The login timeout period expired before the connection to the data source completed. The timeout period is set through SQLSetConnectAttr, SQL_ATTR_LOGIN_TIMEOUT. |
HYT01 | Connection timeout expired | The connection timeout period expired before the data source responded to the request. The connection timeout period is set through SQLSetConnectAttr, SQL_ATTR_CONNECTION_TIMEOUT. |
IM001 | Driver does not support this function | (DM) The driver corresponding to the specified data source name does not support the function. |
IM002 | Data source not found and no default driver specified | (DM) The data source name specified in the browse request connection string (InConnectionString) was not found in the system information, nor was there a default driver specification. (DM) ODBC data source and default driver information could not be found in the system information. |
IM003 | Specified driver could not be loaded | (DM) The driver listed in the data source specification in the system information, or specified by the DRIVER keyword, was not found or could not be loaded for some other reason. |
IM004 | Driver’s SQLAllocHandle on SQL_HANDLE _ENV failed |
(DM) During SQLBrowseConnect, the Driver Manager called the driver’s SQLAllocHandle function with a HandleType of SQL_HANDLE_ENV and the driver returned an error. |
IM005 | Driver’s SQLAllocHandle on SQL_HANDLE _DBC failed |
(DM) During SQLBrowseConnect, the Driver Manager called the driver’s SQLAllocHandle function with a HandleType of SQL_HANDLE_DBC and the driver returned an error. |
IM006 | Driver’s SQLSetConnectAttr failed | (DM) During SQLBrowseConnect, the Driver Manager called the driver’s SQLSetConnectAttr function and the driver returned an error. |
IM009 | Unable to load translation DLL | The driver was unable to load the translation DLL that was specified for the data source or for the connection. |
IM010 | Data source name too long | (DM) The attribute value for the DSN keyword was longer than SQL_MAX_DSN_LENGTH characters. |
IM011 | Driver name too long | (DM) The attribute value for the DRIVER keyword was longer than 255 characters. |
IM012 | DRIVER keyword syntax error | (DM) The keyword-value pair for the DRIVER keyword contained a syntax error. |
Comments
A browse request connection string has the following syntax:
connection-string ::= attribute[;] | attribute; connection-string
attribute ::= attribute-keyword=attribute-value | DRIVER=[{]attribute-value[}]
attribute-keyword ::= DSN | UID | PWD
| driver-defined-attribute-keyword
attribute-value ::= character-string
driver-defined-attribute-keyword ::= identifier
where character-string has zero or more characters; identifier has one or more characters; attribute-keyword is not case-sensitive; attribute-value may be case-sensitive; and the value of the DSN keyword does not consist solely of blanks. Because of connection string and initialization file grammar, keywords and attribute values that contain the characters []{}(),;?*=!@ should be avoided. Because of the grammar in the system information, keywords and data source names cannot contain the backslash (\) character. For an ODBC 2.x driver, braces are required around the attribute value for the DRIVER keyword.
If any keywords are repeated in the browse request connection string, the driver uses the value associated with the first occurrence of the keyword. If the DSN and DRIVER keywords are included in the same browse request connection string, the Driver Manager and driver use whichever keyword appears first.
For information about how an application chooses a data source or driver, see “Choosing a Data Source or Driver” in Chapter 6, “Connecting to a Data Source or Driver.”
The browse result connection string is a list of connection attributes. A connection attribute consists of an attribute keyword and a corresponding attribute value. The browse result connection string has the following syntax:
connection-string ::= attribute[;] | attribute; connection-string
attribute ::= [*]attribute-keyword=attribute-value
attribute-keyword ::= ODBC-attribute-keyword
| driver-defined-attribute-keyword
ODBC-attribute-keyword = {UID | PWD}[:localized-identifier]
driver-defined-attribute-keyword ::= identifier[:localized-identifier]
attribute-value ::= {attribute-value-list} | ?
(The braces are literal; they are returned by the driver.)
attribute-value-list ::= character-string [:localized-character string] | character-string [:localized-character string], attribute-value-list
where character-string and localized-character string have zero or more characters; identifier and localized-identifier have one or more characters; attribute-keyword is not case-sensitive; and attribute-value may be case-sensitive. Because of connection string and initialization file grammar, keywords, localized identifiers, and attribute values that contain the characters []{}(),;?*=!@ should be avoided. Because of the grammar in the system information, keywords and data source names cannot contain the backslash (\) character.
The browse result connection string syntax is used according to the following semantic rules:
SQLBrowseConnect requires an allocated connection. The Driver Manager loads the driver that was specified in or that corresponds to the data source name specified in the initial browse request connection string; for information about when this occurs, see the “Comments” section in SQLConnect. It may establish a connection with the data source during the browsing process. If SQLBrowseConnect returns SQL_ERROR, outstanding connections are terminated and the connection is returned to an unconnected state.
Note SQLBrowseConnect does not support connection pooling. If SQLBrowseConnect is called while connection pooling is enabled, SQLSTATE HY000 (General error) will be returned.
When SQLBrowseConnect is called for the first time on a connection, the browse request connection string must contain the DSN keyword or the DRIVER keyword. If the browse request connection string contains the DSN keyword, the Driver Manager locates a corresponding data source specification in the system information:
If the browse request connection string contains the DRIVER keyword, the Driver Manager loads the specified driver; it does not attempt to locate a data source in the system information. Because the DRIVER keyword does not use information from the system information, the driver must define enough keywords so that a driver can connect to a data source using only the information in the browse request connection strings.
On each call to SQLBrowseConnect, the application specifies the connection attribute values in the browse request connection string. The driver returns successive levels of attributes and attribute values in the browse result connection string; it returns SQL_NEED_DATA as long as there are connection attributes that have not yet been enumerated in the browse request connection string. The application uses the contents of the browse result connection string to build the browse request connection string for the next call to SQLBrowseConnect. All mandatory attributes (those not preceded by an asterisk in the OutConnectionString argument) must be included in the next call to SQLBrowseConnect. Note that the application cannot use the contents of previous browse result connection strings when building the current browse request connection string; that is, it cannot specify different values for attributes set in previous levels.
When all levels of connection and their associated attributes have been enumerated, the driver returns SQL_SUCCESS, the connection to the data source is complete, and a complete connection string is returned to the application. The connection string is suitable to use in conjunction with SQLDriverConnect with the SQL_DRIVER_NOPROMPT option to establish another connection. The complete connection string cannot be used in another call to SQLBrowseConnect, however; if SQLBrowseConnect were called again, the entire sequence of calls would have to be repeated.
SQLBrowseConnect also returns SQL_NEED_DATA if there are recoverable, nonfatal errors during the browse process; for example, an invalid password or attribute keyword supplied by the application. When SQL_NEED_DATA is returned and the browse result connection string is unchanged, an error has occurred and the application can call SQLGetDiagRec to return the SQLSTATE for browse-time errors. This permits the application to correct the attribute and continue the browse.
An application can terminate the browse process at any time by calling SQLDisconnect. The driver will terminate any outstanding connections and return the connection to an unconnected state.
For more information, see “Connecting With SQLBrowseConnect” in Chapter 6, “Connecting to a Data Source or Driver.”
If a driver supports SQLBrowseConnect, the driver keyword section in the system information for the driver must contain the ConnectFunctions keyword with the third character set to “Y.”
Code Example
In the following example, an application calls SQLBrowseConnect repeatedly. Each time SQLBrowseConnect returns SQL_NEED_DATA, it passes back information about the data it needs in *OutConnectionString. The application passes OutConnectionString to its routine GetUserInput (not shown). GetUserInput parses the information, builds and displays a dialog box, and returns the information entered by the user in *InConnectionString. The application passes the user’s information to the driver in the next call to SQLBrowseConnect. After the application has provided all necessary information for the driver to connect to the data source, SQLBrowseConnect returns SQL_SUCCESS and the application proceeds.
For a more detailed example of connecting to a SQL Server driver by calling SQLBrowseConnect, see “SQL Server Browsing Example” in Chapter 6, “Connecting to a Data Source or Driver.”
For example, to connect to the data source Sales, the following actions might occur. First, the application passes the following string to SQLBrowseConnect:
"DSN=Sales"
The Driver Manager loads the driver associated with the data source Sales. It then calls the driver’s SQLBrowseConnect function with the same arguments it received from the application. The driver returns the following string in *OutConnectionString:
"HOST:Server={red,blue,green};UID:ID=?;PWD:Password=?"
The application passes this string to its GetUserInput routine, which builds a dialog box that asks the user to select the red, blue, or green server, and to enter a user ID and password. The routine passes the following user-specified information back in *InConnectionString, which the application passes to SQLBrowseConnect:
"HOST=red;UID=Smith;PWD=Sesame"
SQLBrowseConnect uses this information to connect to the red server as Smith with the password Sesame, and then returns the following string in *OutConnectionString:
"*DATABASE:Database={SalesEmployees,SalesGoals,SalesOrders}"
The application passes this string to its GetUserInput routine, which builds a dialog box that asks the user to select a database. The user selects empdata and the application calls SQLBrowseConnect a final time with this string:
"DATABASE=SalesOrders"
This is the final piece of information the driver needs to connect to the data source; SQLBrowseConnect returns SQL_SUCCESS and *OutConnectionString contains the completed connection string:
"DSN=Sales;HOST=red;UID=Smith;PWD=Sesame;DATABASE=SalesOrders"
#define BRWS_LEN 100
SQLHENV henv;
SQLHDBC hdbc;
SQLHSTMT hstmt;
SQLRETURN retcode;
SQLCHAR szConnStrIn[BRWS_LEN], szConnStrOut[BRWS_LEN];
SQLSMALLINT cbConnStrOut;
/* Allocate the environment handle. */
retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
/* Set the version environment attribute. */
retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, SQL_OV_ODBC3, 0);
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
/* Allocate the connection handle. */
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
/* Call SQLBrowseConnect until it returns a value other than */
/* SQL_NEED_DATA (pass the data source name the first time). */
/* If SQL_NEED_DATA is returned, call GetUserInput (not */
/* shown) to build a dialog from the values in szConnStrOut. */
/* The user-supplied values are returned in szConnStrIn, */
/* which is passed in the next call to SQLBrowseConnect. */
lstrcpy(szConnStrIn, "DSN=Sales");
do {
retcode = SQLBrowseConnect(hdbc, szConnStrIn, SQL_NTS,
szConnStrOut, BRWS_LEN, &cbConnStrOut);
if (retcode == SQL_NEED_DATA)
GetUserInput(szConnStrOut, szConnStrIn);
} while (retcode == SQL_NEED_DATA);
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO){
/* Allocate the statement handle. */
retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
/* Process data after successful connection */
...;
...;
...;
SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
}
SQLDisconnect(hdbc);
}
}
SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
}
}
SQLFreeHandle(SQL_HANDLE_ENV, henv);
Related Functions
For information about | See |
Allocating a connection handle | SQLAllocHandle |
Connecting to a data source | SQLConnect |
Disconnecting from a data source | SQLDisconnect |
Connecting to a data source using a connection string or dialog box | SQLDriverConnect |
Returning driver descriptions and attributes | SQLDrivers |
Freeing a connection handle | SQLFreeHandle |