4D v16

Connecting to an Oracle database

Home

 
4D v16
Connecting to an Oracle database

Connecting to an Oracle database  


 

 

This 4D method allows a user to connect to an Oracle database. To keep the code simple, we have not included any error-handling.

Although this code may seem a bit long for a simple connection, you need to keep in mind that 4D for OCI also includes a simpler alternative by means of the OCILogon command, which lets you avoid many of the handle assignments and helps to simplify and shorten your code. The example shown below is primarily for didactic purposes.

This method starts by assigning the various handles, in hierarchical order. All handles are assigned with respect to the environment handle.

OCIServerAttach creates an access to the Oracle server by associating a connection string to the server handle.
OCISessionBegin starts the session itself, by establishing the connection.

Source code for OCI_CONNECT project method:

  //Method: CONNECT
  //Example of method call: CONNECT ("SCOTT";"TIGER";"ORAQA")
  //Use: connects a user to an Oracle database
  //$1: user name
  //$2: password
  //$3: connection string or name of Oracle service specified in 'tnsnames.ora' file
 
 C_TEXT(${1})
 C_LONGINT(envhp//environment handle
 C_LONGINT(svchp//context handle
 C_LONGINT(authp//session handle
 C_LONGINT(srvhp//server handle
 C_LONGINT($status//return of OCI commands
 
  //Allocation of handles
 $status:=OCIEnvCreate(envhp;OCI_DEFAULT) //environment handle. 'Default' environment
 
  //(No processing of objects...)
 $status:=OCIHandleAlloc(envhp;errhp;OCI_HTYPE_ERROR) //handle of errors (if any)
 $status:=OCIHandleAlloc(envhp;svchp;OCI_HTYPE_SVCCTX) //context handle
 $status:=OCIHandleAlloc(envhp;authp;OCI_HTYPE_SESSION) //session handle
 $status:=OCIHandleAlloc(envhp;srvhp;OCI_HTYPE_SERVER) //server handle
 
  //create access to server by assigning connection string to the server handle
 $status:=OCIServerAttach(srvhp;errhp;$3)
 
  //assign server handle to context handle
 $status:=OCIAttrSetVal(svchp;srvhp;OCI_ATTR_SERVER;errhp)
 
  //update name and password attributes of session handle
  //with parameters supplied to this method
 $status:=OCIAttrSetText(authp;$1;OCI_ATTR_USERNAME;errhp)
 $status:=OCIAttrSetText(authp;$2;OCI_ATTR_PASSWORD;errhp)
 
  //assign session handle to context handle
 $status:=OCIAttrSetVal(svchp;authp;OCI_ATTR_SESSION;errhp)
 
  //start of user session
 $status:=OCISessionBegin(svchp;errhp;authp;OCI_CRED_RDBMS;OCI_DEFAULT)

 
EIGENSCHAFTEN 

Produkt: 4D
Thema: Examples of use

 
GESCHICHTE 

 
ARTIKELVERWENDUNG

4D for OCI ( 4D v16)