/******************************************************************************
**
** Source File Name = drivrcon.c 1.3
**
** Licensed Materials - Property of IBM
**
** (C) COPYRIGHT International Business Machines Corp. 1995, 1999
** All Rights Reserved.
**
** US Government Users Restricted Rights - Use, duplication or
** disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
**
**
** PURPOSE :
** - demonstrate basic connection to two datasources using SQLDriverConnect
** - error handling ignored for simplicity
** Refer to multiccon.c for an expanded version.
**
** For more information about these samples see the README file.
**
** For more information on programming in CLI see the:
** - "Building CLI Applications" section of the Application Building Guide, and the
** - CLI Guide and Reference.
**
** For more information on the SQL language see the SQL Reference.
**
********************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sqlcli1.h>
#include "samputil.h" /* Header file for CLI sample code */
/* For the Macintosh environment when generating 68K applications */
#ifdef DB268K
/* Need to include ASLM for 68K applications */
#include <LibraryManager.h>
#endif
#define MAX_CONNECTIONS 2
/*
* Global Variables for user id and password.
* To keep samples simple, not a recommended practice.
*/
extern SQLCHAR server[SQL_MAX_DSN_LENGTH + 1] ;
extern SQLCHAR uid[MAX_UID_LENGTH + 1] ;
extern SQLCHAR pwd[MAX_PWD_LENGTH + 1] ;
int
drv_connect(SQLHENV henv,
SQLHDBC * hdbc,
SQLCHAR con_type);
/*******************************************************************
** main
** - initialize
** - terminate
*******************************************************************/
int main( int argc, char * argv[] ) {
SQLHANDLE henv, hdbc[MAX_CONNECTIONS] ;
SQLRETURN rc ;
/* For the Macintosh environment when generating 68K applications */
#ifdef DB268K
/*
* Before making any API calls for 68K environment,
* need to initialize the Library Manager
*/
InitLibraryManager(0,kCurrentZone,kNormalMemory);
atexit(CleanupLibraryManager);
#endif
/* allocate an environment handle */
SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv ) ;
/*
* Before allocating any connection handles, set Environment wide
* Connect Options
* Set to Connect Type 2, Syncpoint 1
*/
if ( SQLSetEnvAttr( henv,
SQL_CONNECTTYPE,
( SQLPOINTER ) SQL_COORDINATED_TRANS,
0
) != SQL_SUCCESS ) {
printf( ">---ERROR while setting Connect Type 2 -------------\n" ) ;
return( SQL_ERROR ) ;
}
if ( SQLSetEnvAttr( henv,
SQL_SYNC_POINT,
( SQLPOINTER ) SQL_ONEPHASE,
0
) != SQL_SUCCESS ) {
printf( ">---ERROR while setting Syncpoint One Phase -------------\n" ) ;
return( SQL_ERROR ) ;
}
/* Connect to first data source */
drv_connect( henv, &hdbc[0], '1' ) ;
/* Connect to second data source */
drv_connect( henv, &hdbc[1], '1' ) ;
/********* Start Processing Step *************************/
/* allocate statement handle, execute statement, etc. */
/********* End Processing Step ***************************/
/* Disconnect, free handles and exit */
printf( "\nHit Enter to disconnect\n" ) ;
getchar() ;
printf( "\n>Disconnecting .....\n" ) ;
/* disconnect first connection */
rc = SQLDisconnect( hdbc[0] ) ;
CHECK_HANDLE( SQL_HANDLE_DBC, hdbc[0], rc ) ;
/* disconnect second connection */
rc = SQLDisconnect( hdbc[1] ) ;
CHECK_HANDLE( SQL_HANDLE_DBC, hdbc[1], rc ) ;
/* free first connection handle */
rc = SQLFreeHandle( SQL_HANDLE_DBC, hdbc[0] ) ;
CHECK_HANDLE( SQL_HANDLE_DBC, hdbc[0], rc ) ;
/* free second connection handle */
rc = SQLFreeHandle( SQL_HANDLE_DBC, hdbc[1] ) ;
CHECK_HANDLE( SQL_HANDLE_DBC, hdbc[1], rc ) ;
/* free environment handle */
rc = SQLFreeHandle( SQL_HANDLE_ENV, henv ) ;
if ( rc != SQL_SUCCESS ) return( terminate( henv, rc ) ) ;
return( SQL_SUCCESS ) ;
}
/*--> SQLL1X34.SCRIPT */
/********************************************************************
** drv_connect - Prompt for connect options and connect **
********************************************************************/
int
drv_connect(SQLHENV henv,
SQLHDBC * hdbc,
SQLCHAR con_type)
{
SQLRETURN rc;
SQLCHAR server[SQL_MAX_DSN_LENGTH + 1];
SQLCHAR uid[MAX_UID_LENGTH + 1];
SQLCHAR pwd[MAX_PWD_LENGTH + 1];
SQLCHAR con_str[255];
SQLCHAR buffer[255];
SQLSMALLINT outlen;
printf("Enter Server Name:\n");
gets((char *) server);
printf("Enter User Name:\n");
gets((char *) uid);
printf("Enter Password Name:\n");
gets((char *) pwd);
/* Allocate a connection handle */
rc = SQLAllocHandle( SQL_HANDLE_DBC,
henv,
hdbc
);
CHECK_HANDLE( SQL_HANDLE_DBC, *hdbc, rc);
sprintf((char *)con_str, "DSN=%s;UID=%s;PWD=%s;AUTOCOMMIT=0;CONNECTTYPE=1;",
server, uid, pwd);
rc = SQLDriverConnect(*hdbc,
(SQLHWND) NULL,
con_str,
SQL_NTS,
NULL, 0, NULL,
SQL_DRIVER_NOPROMPT);
if (rc != SQL_SUCCESS) {
printf("Error while connecting to database, RC= %ld\n", rc);
CHECK_HANDLE( SQL_NULL_HENV, *hdbc, rc);
return (SQL_ERROR);
} else {
printf("Successful Connect\n");
return (SQL_SUCCESS);
}
}
/*<-- */