Appendix A. Sample applications 261
/* get connection information from command line arguments */
strcpy(ip, argv[1]);
strcpy(port, argv[2]);
strcpy(dbname, argv[3]);
strcpy(user, argv[4]);
strcpy(passwd, argv[5]);
/* populate the connection string */
sprintf((char *)connStr,
"Database=%s; Protocol=tcpip; Hostname=%s; Servicename=%s; Uid=%s;
Pwd=%s",
dbname, ip, port, user, passwd);
/* allocate an environment handle */
cliRC = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
if (cliRC != SQL_SUCCESS)
{
printf("\n ERROR while allocating the environment handle.\n");
return 1;
}
/* allocate a connection handle */
cliRC = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
if (cliRC != SQL_SUCCESS)
{
printf("\n ERROR while allocating the connection handle.\n");
return 1;
}
printf("\n Connecting to the database %s ...\n", dbname);
/* connect to database using dsn-less connection */
cliRC = SQLDriverConnect(hdbc, (SQLHWND)NULL, connStr, SQL_NTS, NULL, 0,
NULL, SQL_DRIVER_NOPROMPT);
if (cliRC != SQL_SUCCESS )
{printf ("\n Failed to connect to the database %s.\n", dbname);
/* get the first field settings of diagnostic record */
cliRC = SQLGetDiagRec(SQL_HANDLE_DBC, hdbc, 1, sqlstate, &sqlcode,
message, SQL_MAX_MESSAGE_LENGTH + 1, &length);
printf("\n SQLSTATE = %s", sqlstate);
printf("\n SQLCODE = %d", sqlcode);
printf("\n Message: %s", message);
}else
{printf("\n Connected to the database %s.\n", dbname);