CONNECT statement

Establishes a connection to one or more databases from within an ABL procedure or class.

Note: OpenEdge identifies all connected databases for access within an external procedure or class at the start of execution for each compilation unit. Therefore, you cannot directly connect a database using this statement and directly access tables in the database from within the same external procedure or class. Instead, you can directly connect the database in one procedure or class, and from this one call another external subprocedure, or instantiate another class, that accesses the database tables.

Syntax

CONNECT
  {
    { physical-name | VALUE ( expression ) } [ options ] |options
  }
  [ NO-ERROR ]
physical-name
The actual name of the database on disk. It can be a simple filename, relative pathname, or a fully qualified pathname, represented as an unquoted string, or a quoted string. If you do not give a fully qualified pathname, the AVM searches for the database relative to your current working directory. The database name is restricted to alphanumeric characters. Diacritical marks and the symbols \ " ' * ; | ? [ ] ( ) ! { } < > @ + = : ~ are not permitted.
VALUE ( expression )
A character expression (a quoted string, field name, variable name, or similar expression) whose value starts with the Physical Database Name (-db) connection parameter followed by zero or more of the same client connection parameters that you can specify in options.
options
One or more client connection parameters (unquoted), similar to those used to start OpenEdge. Valid options are a subset of OpenEdge startup parameters that include all client database connection parameters. If you specify options without physical-name or VALUE (expression), the first database connection parameter must be the Physical Database Name (-db) parameter. The specification of the User ID (-U) parameter (and Password (-P) parameter, if required), determines the user identity for the connection, and its tenancy (if the database is multi-tenant). Note that these (and all connection) parameters are case sensitive.
If you need to use an argument with the special characters (diacritical marks and the symbols \ " ' * ; | ? [ ] ( ) ! { } < > @ + = : ~), the argument must be quoted for the AVM to handle it properly. The following code shows one way to apply this for a password, vpasswd, that may contain special characters:
connString = "-db test -U " + vUser + " -P " + QUOTER(vpasswd).

CONNECT VALUE(connString)
Caution:
If you do not specify -U and -P, for backward compatibility, OpenEdge attempts to connect the database with a default connection identity. This default connection identity can be set using either the blank ("") user ID or the user ID of the operating system process in which the AVM is running. The user ID set for the default connection identity depends on the domain configuration in the database. For more information on connecting with a default identity, see the User ID (-U) parameter description in OpenEdge Deployment: Startup Command and Parameter Reference. For more information on the effects of connecting a database with a default identity, see the Notes of this statement entry.

For more information on all database client connection parameters, see OpenEdge Deployment: Startup Command and Parameter Reference.

NO-ERROR
Suppresses ABL errors or error messages that would otherwise occur and diverts them to the ERROR-STATUS system handle. If an error occurs, the action of the statement is not done and execution continues with the next statement. If the statement fails, any persistent side-effects of the statement are backed out. If the statement includes an expression that contains other executable elements, like methods, the work performed by these elements may or may not be done, depending on the order the AVM resolves the expression elements and the occurrence of the error.

For the CONNECT statement with NO-ERROR, the option does not suppress all errors produced by the server; only errors caused by the CONNECT statement itself. For example, if the server to which you are connecting runs out of resources, its error message will not be suppressed. If a CONNECT error occurs (for example, the database does not exist or is in use in single-user mode), error information is written to the ERROR-STATUS system handle.

To check for errors after a statement that uses the NO-ERROR option:

  • Check the ERROR-STATUS:ERROR attribute to see if the AVM raised the ERROR condition.
  • Check if the ERROR-STATUS:NUM-MESSAGES attribute is greater than zero to see if the AVM generated error messages. ABL handle methods used in a block without a CATCH end block treat errors as warnings and do not raise ERROR, do not set the ERROR-STATUS:ERROR attribute, but do add messages to the ERROR-STATUS system handle. Therefore, this test is the better test for code using handle methods without CATCH end blocks. ABL handle methods used in a block with a CATCH end block raise ERROR and add messages to the error object generated by the AVM. In this case, the AVM does not update the ERROR-STATUS system handle.
  • Use ERROR-STATUS:GET-MESSAGE( message-num ) to retrieve a particular message, where message-num is 1 for the first message.

If the statement does not include the NO-ERROR option, you can use a CATCH end block to handle errors raised by the statement.

Some other important usage notes on the NO-ERROR option:

  • NO-ERROR does not suppress errors that raise the STOP or QUIT condition.
  • A CATCH statement, which introduces a CATCH end block, is analogous to a NO-ERROR option in that it also suppresses errors, but it does so for an entire block of code. It is different in that the error messages are contained in a class-based error object (generated by the AVM or explicitly thrown), as opposed to the ERROR-STATUS system handle. Also, if errors raised in the block are not handled by a compatible CATCH block, ON ERROR phrase, or UNDO statement, then the error is not suppressed, but handled with the default error processing for that block type.
  • When a statement contains the NO-ERROR option and resides in a block with a CATCH end block, the NO-ERROR option takes precedence over the CATCH block. That is, an error raised on the statement with the NO-ERROR option will not be handled by a compatible CATCH end block. The error is redirected to the ERROR-STATUS system handle as normal.
  • If an error object is thrown to a statement that includes the NO-ERROR option, then the information and messages in the error object will be used to set the ERROR-STATUS system handle. This interoperability feature is important for those integrating code that uses the traditional NO-ERROR technique with the newer, structured error handling that features error objects and CATCH end blocks.

Examples

This procedure attempts to connect to databases mydb1 and mydb2 in single-user mode, with error suppression. You must connect to a database before you run a procedure that references it.

r-connct.p

CONNECT mydb1 -1 -db mydb2 -1 NO-ERROR.

The following four code fragments attempt exactly the same database connection to the Sports2000 database:

CONNECT C:\OpenEdge\WRK\db\Sports2000 -H dbserver -S 1900 NO-ERROR.
CONNECT -db C:\OpenEdge\WRK\db\Sports2000 -H dbserver -S 1900
 NO-ERROR.
CONNECT VALUE("-db C:\OpenEdge\WRK\db\Sports2000 -H dbserver
   -S 1900")
 NO-ERROR.
CONNECT VALUE("-db C:\OpenEdge\WRK\db\Sports2000 -H dbserver")
   -S 1900
  NO-ERROR.

The following procedure fragment shows how you can use the VALUE option to specify a user ID (cUserID) and password (cPasswd) that a user might enter in response to a prompt to authenticate the same database connection:

DEFINE INPUT PARAMETER cUserID AS CHARACTER NO-UNDO.
DEFINE INPUT PARAMETER cPasswd AS CHARACTER NO-UNDO.

CONNECT C:\OpenEdge\WRK\db\Sports2000 
  VALUE( "-U " + cUserID +
        " -P " + "oech1::" + AUDIT-POLICY:ENCRYPT-AUDIT-MAC-KEY(cPasswd))
  -H dbserver -S 1900 NO-ERROR.

Note also that this fragment encrypts the password value (cPasswd) and concatenates it with a prefix in a form that OpenEdge expects for encrypted passwords. For more information, see the ENCRYPT-AUDIT-MAC-KEY( ) method reference entry.

In the next example, assume database sports2000 has not been previously connected, so the following r-cnct1.p procedure fails. At the start of execution, r-cnct1.p checks whether sports2000 is connected. If sports2000 is not connected, a run-time error occurs. As shown in the example, attempting to connect to sports2000 within the procedure does not solve the problem:

r-cnct1.p

/* NOTE: this code does NOT work */

CONNECT sports2000 -1.
FOR EACH sports2000.Customer NO-LOCK:
  DISPLAY Customer.
END.

Instead, split r-cnct1.p into two procedures, as shown in r-dispcu.p and r-cnct2.p:

r-dispcu.p

FOR EACH sports2000.Customer NO-LOCK:
  DISPLAY Customer.
END.

r-cnct2.p

CONNECT sports2000 -1.
RUN r-dispcu.p.

This time, database sports2000 is connected before r-dispcu.p is invoked, so r-dispcu.p runs successfully.

Notes

See also

ALIAS function, CONNECTED function, CREATE ALIAS statement, CREATE CALL statement, DATASERVERS function, DBCODEPAGE function, DBCOLLATION function, DBRESTRICTIONS function, DBTYPE function, DBVERSION function, DELETE ALIAS statement, DISCONNECT statement, FRAME-DB function, LDBNAME function, NUM-DBS function, PDBNAME function, SDBNAME function, SET-DB-CLIENT function, SETUSERID function