Share via


SQLSTRINGCONNECT( ) Function

Establishes a connection to a data source through a connection string.

SQLSTRINGCONNECT([cConnectString])

Return Values

Numeric

Parameters

  • cConnectString
    Specifies the data source connection string required by some ODBC drivers. Visual FoxPro passes the connection string to the ODBC driver. For more information about data source connection strings, see your ODBC driver documentation.

    If SQLSTRINGCONNECT( ) is issued without cConnectString, the SQL Data Sources dialog box is displayed, making it possible for you to choose a data source.

Remarks

SQLSTRINGCONNECT( ) 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.

Example

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

If you successfully connect to the data source, SQLSTRINGCONNECT( ) 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, SQLSTRINGCONNECT( ) returns a negative number and a message is displayed.

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

The following examples show how you can use the SQLStringConnect command without a Data Source Name (DSN).

lcDSNLess="driver = SQL Server;server=your_server;uid=sa;pwd=sa_pw"

-or-

lcDSNLess="driver = {SQL Server};server=your_server;uid=sa;pwd=sa_pw" 

-or-

lcDSNLess="DRIVER = {SQL Server};" ; 
+ "SERVER=your_server;" ;
+ "UID=your_server_userid;" ;
+ "PWD=your_server_pw;" ;
+ "DATABASE=PUBS;" ;
+ "WSID=your machine name or userid;" ;
+ "APP=MicroX(R) Sample App"

lnConnHandle=sqlstringconnect(m.lcDSNLess)

See Also

AERROR( ) | SQLCONNECT( ) | SQLDISCONNECT( )