Click or drag to resize

Functions.SqlConnect Method (String, Usual, Usual, Usual)

Establishes a connection to a data source.

Namespace:  XSharp.VFP
Assembly:  XSharp.VFP (in XSharp.VFP.dll) Version: 2.17
 FUNCTION SqlConnect(
	cDataSourceName AS STRING,
	 cPassword AS USUAL,
	 lShared AS USUAL
Request Example View Source


Type: String
This specifies an ODBC Datasource name. For non ODBC connections we recomment you use SqlStringConnect() to connect.
Type: Usual
Specifies the user identifier used to log on to the data source. This may be omitted if a userid/password is stored with the ODBC Datasource.
Type: Usual
Specifies the password for gaining access to the data source. This may be omitted if a userid/password is stored with the ODBC Datasource.
Type: Usual
Specifies whether or not to create a shared connection.
False (.F.) - SqlConnect( ) does not create a shared connection. (Default)
True (.T.) - SqlConnect( ) creates a shared connection.

Return Value

Type: Long
Numeric data type. SqlConnect( ) returns a positive nonzero numeric value as the statement handle if you successfully connect to the data source. SqlConnect( ) returns –1 if it cannot make the connection.
You should store this statement handle in a memory variable and use the variable in subsequent function calls that require a connection handle.
The SqlConnect( ) and SqlStringConnect( ) functions return a numeric value as the statement handle rather than a connection handle. You cannot obtain a connection handle directly. You can still set and get connection properties using the SqlSetProp( ) and SqlGetProp( ) functions by passing the statement handle for that connection and the string, "Shared", as arguments. All other SQL functions use a statement handle instead of a connection handle.
If you issue a statement such as codeSqlConnect(cConnectionName, .T.)/code, and a shared connection is already open with the same name, then the settings for that connection do not change to the settings stored for that connection in the database container (DBC). However, the new statement handle will use the statement settings from the DBC.
You must disable the Open Database Connectivity (ODBC) login dialog box to support SQL pass through with Microsoft Transaction Server. To disable the ODBC login dialog box, use the statement codeSqlSetProp(nStatementHandle, 'DispLogin', 3)/code, where cStatementHandle is the statement handle returned by SqlConnect( ). You can also disable the ODBC login dialog box in the Connection Designer.
Example 1
The following example assumes that an ODBC data source called MyFoxSQLNT exists and is available. SqlConnect( ) returns a numeric value, which is stored to a variable named codegnConnHandle/code.
If you successfully connect to the data source, SqlConnect( ) returns a positive number, a dialog box appears, and SqlDisconnect( ) is called to disconnect from the data source.
If you cannot connect to the data source, SqlConnect( ) returns a negative number and displays a message.
1STORE SqlConnect('MyFoxSQLNT', 'myUserID', 'myPassword') TO gnConnHandle
2IF gnConnHandle <= 0
3= MessageBox('Cannot make connection', 16, 'SQL Connect Error')
5= MessageBox('Connection made', 48, 'SQL Connect Message')
6= SqlDisconnect(gnHandle)
Example 2
Each of the following examples create new shared connections. The Choose Data Source Dialog Box appears, and SqlConnect( ) creates the resulting connection as shared.
2SqlConnect( myConnectionName, .T. )
3SqlConnect( myDataSourceName, myUserID, myPassword, .T. )
Example 3
Each of the following examples creates a new statement handle based on an existing shared connection.
1SqlConnect( nStatementHandleValue )
2SqlConnect( myConnectionName, .T. )
See Also