ODBC C Programming Example

This example shows a C implementation of ODBC that connects a client to a Sybase IQ database. You can find this example in the samples directory of the IQ installation.

// ****************************************************
// Copyright (c) 2001-2009 iAnywhere Solutions, Inc.

// Portions copyright (c) 2001-2009 Sybase, Inc.
// All rights reserved. All unpublished rights reserved.

//
*****************************************************
/*
*****************************************************
This sample code is provided AS IS, without warranty or
liability of any kind. You may use, reproduce, modify
and distribute this sample code without limitation, on
the condition that you retain the foregoing copyright
notice and disclaimer as to the original iAnywhere code.

************************************************* */
/* odbcconnect.cpp : Defines the entry point for the
console application. */

    retcode = SQLAllocHandle( SQL_HANDLE_ENV,
SQL_NULL_HANDLE, &env );
    if (retcode == SQL_SUCCESS || retcode ==
SQL_SUCCESS_WITH_INFO) {
	printf( "env allocated\n" );
	/* Set the ODBC version environment attribute */
	retcode = SQLSetEnvAttr( env,
SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0); 
	retcode = SQLAllocHandle( SQL_HANDLE_DBC, env, &dbc
);
	if (retcode == SQL_SUCCESS || retcode ==
SQL_SUCCESS_WITH_INFO) {
	    printf( "dbc allocated\n" );
	    retcode = SQLConnect( dbc, 
		    (SQLCHAR*) "Sybase IQ Demo", SQL_NTS,
		    (SQLCHAR* ) "DBA", SQL_NTS, 
		    (SQLCHAR*) "sql", SQL_NTS );
	    if (retcode == SQL_SUCCESS || retcode ==
SQL_SUCCESS_WITH_INFO) {
		printf( "Connection successful\n" );
	    }
	    SQLDisconnect( dbc );	}	SQLFreeHandle( SQL_HANDLE_DBC, dbc );
    }
    SQLFreeHandle( SQL_HANDLE_ENV, env );
    return 0;
}