SQLConnect(), SQLDisconnect(), SQLStringConnect()
These functions are the first and last ones you'll need to use SQL Pass-Through (the client-server mode in which you send commands directly to the back end). SQLConnect() and SQLStringConnect() let you hook up with remote data via ODBC. SQLDisconnect() closes a connection opened by one of the others. Think of SQLConnect() and SQLStringConnect() as dialing a phone and SQLDisconnect() as hanging up the phone.
Usage |
nConnectionHandle = SQLConnect( cDataSource [, cUserId [, cPassword ] ] ) nConnectionHandle = SQLConnect( [ cNamedConnection ] ) nConnectionHandle = SQLStringConnect( [ cConnectionString ] ) |
Parameter |
Value |
Meaning |
cDataSource |
Character |
The name of a data source as seen in the ODBC Administrator. |
cUserId |
Character |
The user ID with which to log in to the server. |
cPassword |
Character |
The password with which to log in to the server. |
cNamedConnection |
Character |
A named connection from the current database. |
Omitted |
Presents an ODBC dialog allowing the user to choose which ODBC Connection or Data Source to use. |
|
cConnectionString |
Character |
A complete connection string, as needed to connect to the desired server. Specify the data source by including "dsn=cDataSource" in the string or leave it out and specify the driver name and other connection information to use a "DSN-less" connection. |
Omitted |
A different dialog box than the one above is presented, allowing the operator to choose among file and machine data sources. |
|
nConnectionHandle |
–1 |
Connection attempt failed at the connection level. |
–2 |
Connection attempt failed at the environment level. |
|
Positive Number |
Connection succeeded and can be accessed through nConnectionHandle. |
Usage |
nReturnValue = SQLDisconnect( nConnectionHandle ) |
Parameter |
Value |
Meaning |
nConnectionHandle |
Positive |
The connection handle for the connection to be closed. |
0 |
Close all open connections. |
|
nReturnValue |
1 |
Connection successfully closed. |
–1 |
Connection level error. |
|
–2 |
Environmental error. |
This is a documentation bug. There appears to be an error in the help topic for SQLDisconnect; it states "Use SQLCONNECT(nConnectionHandle) to reconnect to existing handles without generating duplicates." However, you get a "Function argument value, type, or count is invalid" error when you pass a numeric value to SQLCONNECT. |
Example |
* We recommend you use something like the Connection Manager * class shown here to manage your connections. This solves * the problem of there being no way to find out what connections * exist. You'll probably want to beef this class up, at least * with a custom error handler. * We created this class as a visual class. The code shown here * was exported by the Class Browser. You'll find this class * in the Downloads as Connect.VCX. ************************************************** *-- Class: connmgr (h:\hacker\testcode\connect.vcx) *-- ParentClass: custom *-- BaseClass: custom *-- Connection Manager. Used to keep track of all active *-- connections. * DEFINE CLASS connmgr AS custom *-- The number of open connections PROTECTED nconnectioncount nconnectioncount = 0 Name = "connmgr" *-- Holds the open connections PROTECTED aconnections[1] *-- Open a connection PROCEDURE openconnection * Open a connection - based on parameters LPARAMETERS cSource, cUserId, cPassword * cSource is either datasource name or named connection LOCAL nHandle DO CASE CASE NOT EMPTY(DBC()) AND ; INDBC(cSource, "CONNECTION") * named connection - go for it nHandle = SQLCONNECT(cSource) CASE TYPE("cUserId") = "C" AND TYPE("cPassword") = "C" nHandle = SQLCONNECT(cSource, cUserId, cPassword) CASE TYPE("cUserId") = "C" nHandle = SQLCONNECT(cSource, cUserId) CASE TYPE("cPassword") = "C" nHandle = SQLCONNECT(cSource, "", cPassword) OTHERWISE nHandle = SQLCONNECT(cSource) ENDCASE IF nHandle > 0 * successful connection * so add this connection to our list This.nConnectionCount = This.nConnectionCount+1 DIMENSION This.aConnections[This.nConnectionCount, 4] This.aConnections[This.nConnectionCount,1] = nHandle This.aConnections[This.nConnectionCount,2] = cSource This.aConnections[This.nConnectionCount,3] = cUserId This.aConnections[This.nConnectionCount,4] = cPassword ENDIF RETURN nHandle ENDPROC *-- Close an open connection PROCEDURE closeconnection * Close an open connection. Make sure to remove it from * the open list LPARAMETERS nHandle * Which connection to close * Find out if we have such a connection LOCAL nConnectionRow, nResult nConnectionRow = ASCAN(This.aConnections, nHandle, -1, ; -1, 1, 8) IF nConnectionRow > 0 * Found it, now get rid of it nResult = SQLDISCONNECT(nHandle) ADEL(This.aConnections, nConnectionRow) This.nConnectionCount = This.nConnectionCount-1 IF This.nConnectionCount > 0 DIMENSION This.aConnections[This.nConnectionCount,4] ENDIF ELSE * Turn off the error handler and send the code for * error we want. This lets AERROR() return the right * information, but keeps the user from seeing a * message. LOCAL cOldError cOldError=ON("ERROR") ON ERROR * ERROR 1466 ON ERROR &cOldError nResult = -2 ENDIF RETURN nResult ENDPROC *-- List all open connections PROCEDURE list * List the open connections in the active window ?"Connection handle", ; "Data Source/Connection","Userid","Password" IF This.nConnectionCount>0 LOCAL nConn FOR nConn = 1 TO This.nConnectionCount ? This.aConnections[nConn, 1] ?? This.aConnections[nConn, 2] AT 19 IF TYPE("This.aConnections[nConn, 3]") = "C" ?? This.aConnections[nConn, 3] AT 42 ENDIF IF TYPE("This.aConnections[nConn, 4]") = "C" ?? This.aConnections[nConn, 4] AT 49 ENDIF ENDFOR ELSE ? "No Open Connections" ENDIF ENDPROC PROCEDURE count * Return the number of connections RETURN This.nConnectionCount ENDPROC PROCEDURE aconnect * Return an array containing all active connection * information. * Since we're already storing it in an array, * this is as simple as copying the array PARAMETER aReturn EXTERNAL ARRAY aReturn LOCAL nReturn && Return value is # of rows or -1 DO CASE CASE TYPE("aReturn[1]") = "U" * Bad parameter, can't do it nReturn = -1 CASE This.nConnectionCount = 0 nReturn = 0 OTHERWISE * Redim the array to get rid of old data DIMENSION aReturn[1] * Copy from the property into the parameter ACOPY(This.aConnections, aReturn) nReturn = ALEN(aReturn, 1) ENDCASE RETURN nReturn ENDPROC ENDDEFINE * *-- EndDefine: connmgr ************************************************** |
See Also |