Click or drag to resize

SqlStringConnect Function (String, Logic)

X#
Establishes a connection to a data source using a connection string.

Namespace:  XSharp.VFP
Assembly:  XSharp.VFP (in XSharp.VFP.dll) Version: 2.21
Syntax
 FUNCTION SqlStringConnect(
	cConnectString AS STRING,
	lShared AS LOGIC
) AS LONG
Request Example View Source

Parameters

cConnectString
Type: String
This specifies the data source connection string required by the Ado.Net connection object. X# passes the connection string to the Ado.Net DBConnect object. For more information about data source connection strings, see your Ado.Net documentation.
You can also choose a data source from the Select Connection or Data Source Dialog Box, which appears when you call SqlStringConnect( ) with an empty connection string, or by passing the overload that only takes the lShared parameter.
lShared
Type: Logic
Specifies if the data source specified with cConnectString has a shared connection. True (.T.) = Shared, False (.F.) = Exclusive, this is the Default. Specifies if the data source that will be opened should be shared or not. True (.T.) = Shared, False (.F.) = Exclusive, this is the Default.

Return Value

Type: Long
Numeric data type. SqlStringConnect( ) returns a positive nonzero numeric value as the statement handle if you successfully connect to the data source. SqlStringConnect( ) 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.
Remarks
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.
SqlStringConnect( ) always creates a new connection when it successfully makes a connection. However, setting the lShared parameter determines whether you can share the connection later. If you specify a connection as shareable by setting lShared to True (.T.), you can share the connection later by calling SqlConnect( ) and passing the numeric value of the connection handle as the first parameter. For more information, see SqlConnect( ) Function.
You can use SqlConnect( ) to obtain a new statement handle on a shared connection that was opened using SqlStringConnect( ).
Examples
Example 1
The following example assumes that an ODBC data source called MyFoxSQLNT exists and is available. SqlStringConnect( ) returns a numeric value, which is stored to a variable named codegnHandle/code. If you successfully connect to the data source, SqlStringConnect( ) 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, SqlStringConnect( ) returns a negative number and displays a message.
X#
1STORE SqlStringConnect('dsn=MyFoxSQLNT;uid=myUserID;pwd=myPassword') TO gnConnHandle
2IF gnConnHandle < 0
3= MessageBox('Cannot make connection', 16, 'SQL Connect Error')
4ELSE
5= MessageBox('Connection made', 48, 'SQL Connect Message')
6= SqlDisconnect(gnHandle)
7ENDIF
Example 2
The following examples show how you can use the SqlStringConnect( ) function without a Data Source Name (DSN).
X#
1lcDSNLess="driver = SQL Server;server=<servername>;uid=<userid>;pwd=<password>"
-or-
X#
1lcDSNLess="driver = {SQL Server};server=<servername>;uid=<userid>;pwd=<password>"
-or-
X#
1lcDSNLess="DRIVER = {SQL Server};" ;
2+ "SERVER=<servername>;" ;
3+ "UID=<userid>;" ;
4+ "PWD=<password>;" ;
5+ "Database=PUBS;" ;
6+ "WSID=<machine name or userid>;" ;
7+ "APP=MicroX(R) Sample App"
8lnConnHandle=SqlStringConnect(m.lcDSNLess)
Example 3
Each of the following examples creates a new shared connection. In the first statement, the Select Connection or Data Source Dialog Box appears and SqlStringConnect( ) creates the resulting connection as shared.
X#
1SqlStringConnect(.T.)
2SqlStringConnect('myConnectionString', .T.)
See Also