The following code fragment, taken from the ctfirst.c migration example program, illustrates opening a Client-Library connection:
#define NORMAL_EXIT (0)
#define ERROR_EXIT (-1)
#define USER “user_name”
#define PASSWORD “server_password”/*
** STRLEN() -- strlen() that returns 0 for NULL pointers.
*/
#define STRLEN(str) ( ((str) == NULL) ? 0 : (strlen(str)) )CS_CONTEXT *context;
CS_CONNECTION *conn;
... deleted initialization code ...
/*
** Step 3.
** Connect to the server.
** 3a. Allocate a CS_CONNECTION structure.
** 3b. Insert the username, password, and other login parameters
** into the connection structure.
** 3c. Call ct_connect(), passing the CS_CONNECTION as an argument.
*//*
** Step 3a.
** Allocate a CS_CONNECTION structure. The CS_CONNECTION replaces
** DB-Library’s LOGINREC and DBPROCESS structures. The LOGINREC
** fields are connection properties in Client-Library.
**
-- login = dblogin();
-- if (login == (LOGINREC *) NULL)
-- {
-- fprintf(ERR_CH, "dblogin() failed. Exiting.\n");
-- dbexit();
-- exit(ERREXIT);
-- }
*/
ret = ct_con_alloc(context, &conn);
EXIT_ON_FAIL(context, ret, "Allocate connection structure failed.");/*
** Step 3b.
** Put the username, password, and other login information into the
** connection structure. We do this with ct_con_props() calls.
** After the connection is open, Client-Library makes these properties
** read-only.
**
** USER and PASSWORD are defined in dbtoctex.h
**
-- DBSETLUSER(login, USER);
-- DBSETLPWD(login, PASSWORD);
-- DBSETLAPP(login, "dbfirst");
*/ret = ct_con_props(conn, CS_SET, CS_USERNAME, USER,
STRLEN(USER), NULL);
EXIT_ON_FAIL(context, ret, "Set connection username failed.");ret = ct_con_props(conn, CS_SET, CS_PASSWORD, PASSWORD,
STRLEN(PASSWORD), NULL);
EXIT_ON_FAIL(context, ret, "Set connection password failed.");ret = ct_con_props(conn, CS_SET, CS_APPNAME, "ctfirst",
STRLEN("ctfirst"), NULL);
EXIT_ON_FAIL(context, ret, "Set connection application name failed.");/*
** Step 3c.
** Call ct_connect() to open the connection. Unlike dbopen(),
** ct_connect() uses a connection structure which is already
** allocated.
**
-- dbproc = dbopen(login, NULL);
-- if (dbproc == (DBPROCESS *) NULL)
-- {
-- fprintf(ERR_CH, "Connect attempt failed. Exiting.\n");
-- dbexit();
-- exit(ERREXIT);
-- }
*/
ret = ct_connect(conn, NULL, 0);
EXIT_ON_FAIL(context, ret, "Connection attempt failed.");
... deleted command code ...
/*
** Step 5.
** Close our connection. CS_UNUSED as the second ct_close() parameter
** requests an "orderly" close. This means that we expect that the
** connection is idle. If we had issued a command to the server, but
** had not read all the results sent by the server, then this call
** would fail.
**
** If ct_close() were to fail here, then the code in EXIT_ON_FAIL()
** calls ct_exit(CS_FORCE_EXIT) to force all connections closed
** before exiting.
**
-- dbclose(dbproc);
*/
ret = ct_close(conn, CS_UNUSED);
EXIT_ON_FAIL(context, ret, "Orderly connection-close failed.");ret = ct_con_drop(conn);
EXIT_ON_FAIL(context, ret, "ct_con_drop() failed.");
... deleted context-level cleanup code ...