Partager via


SQLCONNECT( ) Function

Establishes a connection to a data source.

SQLCONNECT([DataSourceName, cUserID, cPassword | cConnectionName])

Return Values

Numeric

Parameters

  • DataSourceName
    Specifies the name of a data source as defined in your Odbc.ini file.
  • cUserID
    Specifies a user identifier used to log on to the data source.
  • cPassword
    Specifies the password to the data source.
  • cConnectionName
    Specifies a named connection created with CREATE CONNECTION.

Remarks

SQLCONNECT( ) returns a positive non-zero numeric handle if you successfully connect to the data source. You should store this handle in a memory variable and use the variable in subsequent function calls that require a connection handle. SQLCONNECT( ) returns – 2 if the connection cannot be made.

If SQLCONNECT( ) is issued without any of its additional arguments, the Select Connection or Data Source dialog box can be displayed, allowing you to choose a data source.

Note   The ODBC login dialog must be disabled to support SQL pass through with Microsoft Transaction Server. Use SQLSETPROP(cConnectionHandle, 'DispLogin', 3) to disable the ODBC login dialog (cConnectionHandle is the connection handle returned by SQLCONNECT). The ODBC login dialog can also be disabled in the Connection Designer.

Example

The following example assumes an ODBC data source called MyFoxSQLNT is available, and the user ID for the data source is "sa." SQLCONNECT( ) is issued, and its return value is stored to a variable named gnConnHandle.

If you successfully connect to the data source, SQLCONNECT( ) returns a positive number, a dialog is displayed and SQLDISCONNECT( ) is used to disconnect from the data source.

If you cannot connect to the data source, SQLCONNECT( ) returns a negative number and a message is displayed.

STORE SQLCONNECT('MyFoxSQLNT', 'sa') TO gnConnHandle
IF gnConnHandle <= 0
   = MESSAGEBOX('Cannot make connection', 16, 'SQL Connect Error')
ELSE
   = MESSAGEBOX('Connection made', 48, 'SQL Connect Message)
   = SQLDISCONNECT(gnConnHandle)
ENDIF

See Also

AERROR( ) | CREATE CONNECTION | SQLDISCONNECT( ) | SQLGETPROP( ) | SQLEXEC( ) | SQLSETPROP( ) | SQLSTRINGCONNECT( )