/*******************************************************************************
**
** Source File Name = dbconn.c 1.4
**
** 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 :
** Shows how connect to/disconnect from a database.
**
** 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 "utilcli.h" /* Header file for CLI sample code */
int DbBasicConnect( SQLHANDLE , char *, char *, char * ) ;
int DbDriverConnect( SQLHANDLE , char *, char *, char * ) ;
int DbBrowseConnect( SQLHANDLE , char *, char *, char * ) ;
/*******************************************************************
** main
*******************************************************************/
int main( int argc, char * argv[] )
{ SQLRETURN sqlrc = SQL_SUCCESS;
int rc = 0;
SQLHANDLE henv; /* environment handle */
SQLHANDLE hdbc; /* connection handle */
char dbAlias[SQL_MAX_DSN_LENGTH + 1] ;
char user[MAX_UID_LENGTH + 1] ;
char pswd[MAX_PWD_LENGTH + 1] ;
/* checks the command line arguments */
rc = CmdLineArgsCheck1( argc, argv, dbAlias, user, pswd );
if ( rc != 0 )
{ return( 1 ) ;
}
printf("\n\nDATABASES: HOW TO CONNECT TO/DISCONNECT FROM A DATABASE.\n");
/* allocate an environment handle */
sqlrc = SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv ) ;
if ( sqlrc != SQL_SUCCESS )
{ printf( "\n--ERROR while allocating the environment handle.\n" ) ;
printf( " sqlrc = %d\n", sqlrc);
printf( " line = %d\n", __LINE__);
printf( " file = %s\n", __FILE__);
return( 1 ) ;
}
/* connect to a database with SQLConnect, */
/* this is the basic connection */
rc = DbBasicConnect( henv, dbAlias, user, pswd ) ;
if ( rc != 0 ) return( rc ) ;
/* connect to a database with SQLDriverConnect */
rc = DbDriverConnect( henv, dbAlias, user, pswd ) ;
if ( rc != 0 ) return( rc ) ;
/* connect to a database with SQLBrowseConnect */
rc = DbBrowseConnect( henv, dbAlias, user, pswd ) ;
if ( rc != 0 ) return( rc ) ;
/* free the environment handle */
sqlrc = SQLFreeHandle( SQL_HANDLE_ENV, henv ) ;
ENV_HANDLE_CHECK( henv, sqlrc ) ;
return( 0 ) ;
} /* end main */
/******************************************************************************
** DbBasicConnect - connect to/ disconnect from a database
** using SQLConnect
******************************************************************************/
int DbBasicConnect( SQLHANDLE henv,
char db1Alias[],
char user[],
char pswd[] )
{ SQLRETURN sqlrc = SQL_SUCCESS;
int rc = 0;
SQLHANDLE hdbc; /* connection handle */
printf("\nUSE THE CLI FUNCTIONS\n");
printf("-SQLAllocHandle\n-SQLConnect\n");
printf("-SQLDisconnect\n-SQLFreeHandle\n");
printf("TO CONNECT TO/DISCONNECT FROM A DATABASE:\n");
/* allocate a database connection handle */
sqlrc = SQLAllocHandle( SQL_HANDLE_DBC, henv, &hdbc ) ;
HANDLE_CHECK( SQL_HANDLE_ENV, henv, sqlrc, &henv, &hdbc ) ;
/* connect to the database */
printf( "\n Connecting to the database %s ...\n", db1Alias ) ;
sqlrc = SQLConnect( hdbc,
(SQLCHAR *)db1Alias, SQL_NTS,
(SQLCHAR *)user, SQL_NTS,
(SQLCHAR *)pswd, SQL_NTS
) ;
HANDLE_CHECK( SQL_HANDLE_DBC, hdbc, sqlrc, &henv, &hdbc ) ;
printf( " Connected to the database %s.\n", db1Alias ) ;
/********* Start using the connection *************************/
/********* Stop using the connection **************************/
/* disconnect from the database */
printf( "\n Disconnecting from the database %s ...\n", db1Alias ) ;
sqlrc = SQLDisconnect( hdbc ) ;
HANDLE_CHECK( SQL_HANDLE_DBC, hdbc, sqlrc, &henv, &hdbc ) ;
printf( " Disconnected from the database %s.\n", db1Alias ) ;
/* free the connection handle */
sqlrc = SQLFreeHandle( SQL_HANDLE_DBC, hdbc ) ;
HANDLE_CHECK( SQL_HANDLE_DBC, hdbc, sqlrc, &henv, &hdbc ) ;
return( 0 ) ;
}
/******************************************************************************
** DbDriverConnect - connect to/ disconnect from a database
** using SQLDriverConnect
******************************************************************************/
int DbDriverConnect( SQLHANDLE henv,
char db1Alias[],
char user[],
char pswd[] )
{ SQLRETURN sqlrc = SQL_SUCCESS;
int rc = 0;
SQLHANDLE hdbc; /* connection handle */
SQLCHAR connStr[255];
printf("\nUSE THE CLI FUNCTIONS\n");
printf("-SQLAllocHandle\n-SQLDriverConnect\n");
printf("-SQLDisconnect\n-SQLFreeHandle\n");
printf("TO CONNECT TO/DISCONNECT FROM A DATABASE:\n");
/* allocate a database connection handle */
sqlrc = SQLAllocHandle( SQL_HANDLE_DBC, henv, &hdbc ) ;
HANDLE_CHECK( SQL_HANDLE_ENV, henv, sqlrc, &henv, &hdbc ) ;
/* connect to the database */
printf( "\n Connecting to the database %s ...\n", db1Alias ) ;
sprintf((char *)connStr,
"DSN=%s;UID=%s;PWD=%s;AUTOCOMMIT=0;CONNECTTYPE=1;",
db1Alias, user, pswd);
sqlrc = SQLDriverConnect(hdbc,
(SQLHWND) NULL,
connStr,
SQL_NTS,
NULL, 0, NULL,
SQL_DRIVER_NOPROMPT);
HANDLE_CHECK( SQL_HANDLE_DBC, hdbc, sqlrc, &henv, &hdbc ) ;
printf( " Connected to the database %s.\n", db1Alias ) ;
/********* Start using the connection *************************/
/********* Stop using the connection **************************/
/* disconnect from the database */
printf( "\n Disconnecting from the database %s ...\n", db1Alias ) ;
sqlrc = SQLDisconnect( hdbc ) ;
HANDLE_CHECK( SQL_HANDLE_DBC, hdbc, sqlrc, &henv, &hdbc ) ;
printf( " Disconnected from the database %s.\n", db1Alias ) ;
/* free the connection handle */
sqlrc = SQLFreeHandle( SQL_HANDLE_DBC, hdbc ) ;
HANDLE_CHECK( SQL_HANDLE_DBC, hdbc, sqlrc, &henv, &hdbc ) ;
return( 0 ) ;
}
/******************************************************************************
** DbBrowserConnect - connect to/ disconnect from a database
** using SQLBrowserConnect
******************************************************************************/
int DbBrowseConnect( SQLHANDLE henv,
char db1Alias[],
char user[],
char pswd[] )
{ SQLRETURN sqlrc = SQL_SUCCESS;
int rc = 0;
SQLHANDLE hdbc; /* connection handle */
SQLCHAR connInStr[255];
SQLCHAR OutStr[1025];
SQLSMALLINT indicator;
int count=1;
printf("\nUSE THE CLI FUNCTIONS:\n");
printf("-SQLBrowseConnect\n");
printf("\nOther CLI FUNCTIONS:\n");
printf("-SQLAllocHandle\n");
printf("-SQLDisconnect\n-SQLFreeHandle\n");
printf("TO CONNECT TO/DISCONNECT FROM A DATABASE:\n");
/* allocate a database connection handle */
sqlrc = SQLAllocHandle( SQL_HANDLE_DBC, henv, &hdbc ) ;
HANDLE_CHECK( SQL_HANDLE_ENV, henv, sqlrc, &henv, &hdbc ) ;
/* connect to the database */
printf( "\n Connecting to the database %s ...\n", db1Alias ) ;
sprintf((char *)connInStr,
"DSN=%s;UID=%s;PWD=%s;AUTOCOMMIT=0;",
db1Alias, user, pswd);
/********* Start using the connection *************************/
sqlrc = SQL_NEED_DATA;
while (sqlrc == SQL_NEED_DATA){
sqlrc = SQLBrowseConnect(hdbc, connInStr,
SQL_NTS, OutStr,
sizeof(OutStr), &indicator);
HANDLE_CHECK( SQL_HANDLE_DBC, hdbc, sqlrc, &henv, &hdbc ) ;
printf( " So far connect %d times to database %s \n", count++,db1Alias ) ;
printf( " OutString: %s\n", OutStr ) ;
if(sqlrc == SQL_NEED_DATA){
printf( " You can provide other connection information here by setting connInStr\n" ) ;
break;
}
if(sqlrc == SQL_SUCCESS){
printf( "\n Connected to the database %s.\n", db1Alias ) ;
}
}
/********* Stop using the connection **************************/
/* disconnect from the database */
printf( "\n Disconnecting from the database %s ...\n", db1Alias ) ;
sqlrc = SQLDisconnect( hdbc ) ;
HANDLE_CHECK( SQL_HANDLE_DBC, hdbc, sqlrc, &henv, &hdbc ) ;
printf( " Disconnected from the database %s.\n", db1Alias ) ;
/* free the connection handle */
sqlrc = SQLFreeHandle( SQL_HANDLE_DBC, hdbc ) ;
HANDLE_CHECK( SQL_HANDLE_DBC, hdbc, sqlrc, &henv, &hdbc ) ;
return( 0 ) ;
}