Try OpenEdge Now
skip to main content
SQL Development
ODBC Client : Configuring data sources : Testing your ODBC connection on UNIX : Linux
 
Linux
cc -o testConnect -I$DLC/odbc/include -L$DLC/odbc/lib -lodbc -lodbcinst test_connect.c
Use the below sample program to test your ODBC connection.
testconnect.c
#include <stdio.h>
#include "sql.h" /* ODBC declarations */
#include "sqlext.h" /* more ODBC declarations */
int main(int argc, char *argv[])
{
SQLRETURN sqlReturn;
SQLHANDLE environmentHandle;
SQLHANDLE connectionHandle;
/* make sure we got at least 3 arguments to the exe */
if (argc < 4)
{
printf("Insufficient parameters provided.\n");
printf("Usage - %s dsn userid password\n", argv[0]);
return 1;
}
else
{
/* got at least 3 arguments to the exe, display */
/* arguments with internal usage */
printf("DSN NAME = %s\n", argv[1]);
printf("USER ID = %s\n", argv[2]);
printf("PASSWORD = ****\n\n"); /* don't show actual */
}
/* allocate an ODBC environment handle */
sqlReturn = SQLAllocHandle(SQL_HANDLE_ENV,
SQL_NULL_HANDLE,
&environmentHandle);
if (sqlReturn != SQL_SUCCESS)
{
printf("Unable to allocate Environment Handle, exiting.\n");
return sqlReturn;
}
/* set the ODBC application version to 3.x */
sqlReturn = SQLSetEnvAttr(environmentHandle,
SQL_ATTR_ODBC_VERSION,
(SQLPOINTER) SQL_OV_ODBC3,
SQL_IS_UINTEGER);
if (sqlReturn != SQL_SUCCESS)
{
printf("Unable to set ODBC Versoin to 3.x, exiting.\n");
SQLFreeHandle(SQL_HANDLE_ENV, environmentHandle);
return sqlReturn;
}
/* allocate a database connection handle */
sqlReturn = SQLAllocHandle(SQL_HANDLE_DBC,
environmentHandle,
&connectionHandle);
if (sqlReturn != SQL_SUCCESS
{
printf("Unable to allocate Connection Handle, exiting.\n");
SQLFreeHandle(SQL_HANDLE_ENV, environmentHandle);
return sqlReturn;
}
/* attempt to connect o the database server */
sqlReturn = SQLConnect(connectionHandle,
(SQLCHAR *)argv[1], /* dsn name */
SQL_NTS, /* name null terminated */
(SQLCHAR *)argv[2], /* user id */
SQL_NTS, /* user id null terminated */
(SQLCHAR *)argv[3], /* user pwd */
SQL_NTS); /* pwd null terminated */
if (sqlReturn == SQL_SUCCESS)
{
printf("Connection to %s successful!\n", argv[1]);
/* now disconnect from server */
sqlReturn = SQLDisconnect(connectionHandle);
if (sqlReturn != SQL_SUCCESS)
{
printf("Unable to disconnect client from %s.\n", argv[1]);
}
}
else
{
printf("Unable to connect to %s.\n", argv[1]);
}
/* do the clean up before exiting */
if (connectionHandle != NULL)
{
SQLFreeHandle(SQL_HANDLE_DBC, connectionHandle);
}
if (environmentHandle != NULL)
{
SQLFreeHandle(SQL_HANDLE_ENV, environmentHandle);
}
return sqlReturn;
}
The test executable, once built, can be used to test the ability to connect to a running database server. The executable will take three parameters: DSN, user id, and user password, as shown:
testConnect sports myuser mypwd
A successful connection, using the test executable, will result in a displayed message similar to the following:
DSN NAME = sports
USER ID = foo
PASSWORD = ****
Connection to sports successful.