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.
{
LOGINREC * login // Pointer to a LOGINREC structure
DBPROCESS * dbproc; // Pointer to a DBPROCESS structure
} REMOTE_DBMS
where
int init_remote (srvproc)
SRV_PROC * srvproc;
// Private data area to keep track of remote database connection.
REMOTE_DBMS * remote;
remote = (REMOTE_DBMS *) srv_alloc ((DBINT)sizeof(*remote));
// 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();
// 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));
// 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);
// 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);