/******************************************************************************* ** ** Source File Name = aphndls.c ** ** Licensed Materials - Property of IBM ** ** (C) COPYRIGHT International Business Machines Corp. 1995, 2000 ** All Rights Reserved. ** ** US Government Users Restricted Rights - Use, duplication or ** disclosure restricted by GSA ADP Schedule Contract with IBM Corp. ** ** ** PURPOSE : ** Shows how to allocate/free handles in CLI applications. ** ** 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 */ /******************************************************************* ** main *******************************************************************/ int main( int argc, char * argv[] ) { SQLRETURN sqlrc = SQL_SUCCESS; int rc = 0; SQLHANDLE henv; /* environment handle */ SQLHANDLE hdbc; /* connection handle */ SQLHANDLE hstmt; /* statement 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\nCLI APP.: HOW TO ALLOCATE/FREE HANDLES.\n"); printf("\nUSE THE CLI FUNCTIONS\n"); printf("-SQLAllocHandle\n-SQLConnect\n"); printf("-SQLDisconnect\n-SQLFreeHandle\n"); printf("TO SHOW HOW TO ALLOCATE/FREE HANDLES:\n"); /* allocate an environment handle */ printf("\n Allocate an environment handle.\n"); 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 ) ; } /* allocate a database connection handle */ printf(" Allocate a database connection handle.\n"); sqlrc = SQLAllocHandle( SQL_HANDLE_DBC, henv, &hdbc ) ; HANDLE_CHECK( SQL_HANDLE_ENV, henv, sqlrc, &henv, &hdbc ) ; /* connect to the database */ printf( " Connecting to the database %s ...\n", dbAlias ) ; sqlrc = SQLConnect( hdbc, (SQLCHAR *)dbAlias, 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", dbAlias ) ; /* allocate one or more statement handles */ printf(" Allocate a statement handle.\n"); sqlrc = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ; HANDLE_CHECK( SQL_HANDLE_DBC, hdbc, sqlrc, &henv, &hdbc ) ; /********* Start using the statement handles *******************/ /********* Stop using the statement handles ********************/ /* free the statement handle */ printf("\n Free the statement handle.\n"); sqlrc = SQLFreeHandle( SQL_HANDLE_STMT, hstmt ) ; HANDLE_CHECK( SQL_HANDLE_STMT, hstmt, sqlrc, &henv, &hdbc ) ; /* disconnect from the database */ printf( " Disconnecting from the database %s ...\n", dbAlias ) ; sqlrc = SQLDisconnect( hdbc ) ; HANDLE_CHECK( SQL_HANDLE_DBC, hdbc, sqlrc, &henv, &hdbc ) ; printf( " Disconnected from the database %s.\n", dbAlias ) ; /* free the connection handle */ printf(" Free the statement handle.\n"); sqlrc = SQLFreeHandle( SQL_HANDLE_DBC, hdbc ) ; HANDLE_CHECK( SQL_HANDLE_DBC, hdbc, sqlrc, &henv, &hdbc ) ; /* free the environment handle */ printf(" Free the environment handle.\n"); sqlrc = SQLFreeHandle( SQL_HANDLE_ENV, henv ) ; ENV_HANDLE_CHECK( henv, sqlrc ) ; return( 0 ) ; } /* end main */