/* ... */
#define MAX_CONNECTIONS 2
int
DBconnect(SQLHENV henv,
SQLHDBC * hdbc,
char * server);
int
main()
{
SQLHENV henv;
SQLHDBC hdbc[MAX_CONNECTIONS];
SQLRETURN rc;
char * svr[MAX_CONNECTIONS] =
{
"KARACHI" ,
"DAMASCUS"
}
/* Allocate an environment handle */
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
/* Before allocating any connection handles, set Environment wide
Connect Attributes */
/* Set to CONNECT(type 2)*/
rc = SQLSetEnvAttr(henv, SQL_CONNECTTYPE,
(SQLPOINTER) SQL_COORDINATED_TRANS, 0);
/* ... */
/* Connect to first data source */
/* Allocate a connection handle */
if (SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc[0]) != SQL_SUCCESS) {
printf(">---ERROR while allocating a connection handle-----\n");
return (SQL_ERROR);
}
/* Connect to first data source (Type-II) */
DBconnect (henv,
&hdbc[0],
svr[0]);
/* Allocate a second connection handle */
if (SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc[1]) != SQL_SUCCESS) {
printf(">---ERROR while allocating a connection handle-----\n");
return (SQL_ERROR);
}
/* Connect to second data source (Type-II) */
DBconnect (henv,
&hdbc[1],
svr[1]);
/********* Start processing step *************************/
/* Allocate statement handle, execute statement, and so on */
/* Note that both connections participate in the disposition*/
/* of the transaction. Note that a NULL connection handle */
/* is passed as all work is committed on all connections. */
/********* End processing step ***************************/
(void)SQLEndTran(SQL_HANDLE_HENV, henv, SQL_COMMIT);
/* Disconnect, free handles and exit */
}
/********************************************************************
** Server is passed as a parameter. Note that USERID and PASSWORD**
** are always NULL. **
********************************************************************/
int
DBconnect(SQLHENV henv,
SQLHDBC * hdbc,
char * server)
{
SQLRETURN rc;
SQLCHAR buffer[255];
SQLSMALLINT outlen;
/* Allocate a connection handle */
SQLAllocHandle(SQL_HANDLE_DBC, henv, hdbc);
rc = SQLConnect(*hdbc, server, SQL_NTS, NULL, SQL_NTS, NULL, SQL_NTS);
if (rc != SQL_SUCCESS) {
printf(">--- Error while connecting to database:
return (SQL_ERROR);
} else {
printf(">Connected to
return (SQL_SUCCESS);
}
}
/* ... */