Connecting to a Remote Database

This example shows one method of connecting to a remote database, as used by the gateway sample application. Since the connection is to a SQL Server database, DB-Library structures and function calls are used to establish the connection and to communicate with the remote database.

  1. Declare a structure to hold connection information for accessing a remote database. This structure is passed to Open Data Services event handlers in the SRV_PROC structure:
    {
        LOGINREC * login     // Pointer to a LOGINREC structure   
        DBPROCESS * dbproc;  // Pointer to a DBPROCESS structure  
    } REMOTE_DBMS
    

    where

    login
    Is a pointer to a LOGINREC structure. This structure is defined by the client and contains login and connection information to connect a client to a server.
    dbproc
    Is a pointer to a DBPROCESS structure. This structure is defined by Open Data Services and contains information to manage communications between the client and the server.
  2. Add the init_remote function to the gateway callback program, GATECBS.C:
    int init_remote (srvproc)
    SRV_PROC * srvproc;
    
  3. Allocate a REMOTE_DBMS information structure, which holds the private data space associated with a remote database connection:
    // Private data area to keep track of remote database connection. 
    
    REMOTE_DBMS * remote;
    remote = (REMOTE_DBMS *) srv_alloc ((DBINT)sizeof(*remote));
    
  4. Use the DB-Library call dblogin to allocate and return a pointer to a LOGINREC structure, which is used to make the connection to the remote server. Put the pointer to LOGINREC in remote->login:
    // Now we'll allocate our LOGINREC structure that     
    // we'll use to make connections to the               
    // remote server. We'll actually open the connection  
    // in the SRV_CONNECT handler.                        
    
    remote->login = dblogin();
    
  5. Use the ODS Library srv_pfield function to copy the user name, password, application name, and language defined in the client's SRV_PROC structure to remote->login:
    // Set the user name, password, application name for the   
    // remote database.                                        
    
    DBSETLUSER (remote->login, srv_pfield(srvproc, SRV_USER, (int *)
    NULL));
    
    DBSETLPWD (remote->login, srv_pfield(srvproc, SRV_PWD, (int *)
    NULL));
    
    DBSETLAPP (remote->login, srv_pfield(srvproc, SRV_APPLNAME, (int *)
    NULL));
    
    DBSETLNATLANG(remote->login, srv_pfield(srvproc, SRV_NATLANG, 
        (int *) NULL));
    
  6. Use the DB-Library dbopen function with the parameters in remote->login and the server name to connect to the host:
    // Try to open a connection to the remote database.         
    
    if ((remote->dbproc = dbopen(remote->login, remote_server))
    == (DBPROCESS *) NULL)
    {
    
    //  Send a message to the Open Data Services client if the remote 
    //  connection cannot be made.                                    
    
    srv_sendmsg    (srvproc,
                   SRV_MSG_ERROR,
                   (DBINT)REMOTE_FAIL,
                   (DBTINYINT)0,
                   (DBTINYINT)0,
                   NULL,
                   0,
                   0,
                   "Login to remote DBMS failed.",
                   SRV_NULLTERM);
    
    //  Deallocate remote structure and set the user data pointer in  
    //  the SRV_PROC connection structure to NULL so that the         
    //  disconnect handler won't try to disconnect from the remote    
    //  database.                                                     
    srv_free (remote);
    srv_setuserdata (srvproc, (BYTE *) NULL);
    
  7. After the connection is made, save the remote data structure in the SRV_PROC connection structure so that it will be available to the other handlers using srv_setuserdata and dbsetuserdata, and return the SRV_CONTINUE constant:
    // Connection to the remote database is successful. Save remote    
    // data structure in the SRV_PROC connection structure so that it  
    // will be available to the other handlers. Map the remote         
    // database connection to the SRV_PROC connection structure.       
    
    srv_setuserdata(srvproc, (VOID *)remote);
    dbsetuserdata(remote->dbproc, (VOID *)srvproc);