123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- void returncode_check (SQLHDBC handle,
- SQLHSTMT hstmt,
- int rc,
- char *str);
- static void rc_to_str (int rc,
- char *str);
- void server_connect (
- SQLCHAR *datasource,
- SQLHENV *henv,
- SQLHDBC *hdbc
- )
- {
- int rc;
- SDWORD dbms_err;
- SWORD length;
- unsigned char err_msg[SQL_MAX_MESSAGE_LENGTH];
- unsigned char state[6];
-
- printf ("Connecting to datasource %s ...\n", datasource);
-
- rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, henv);
- if (rc != SQL_SUCCESS)
- {
- printf ("SQLAllocHandle failed with %d\n", rc);
- exit (1);
- }
-
- rc = SQLSetEnvAttr (*henv, SQL_ATTR_ODBC_VERSION,
- (SQLPOINTER) SQL_OV_ODBC3, 0);
- if (rc != SQL_SUCCESS)
- {
- printf ("SQLSetEnvAttr failed with %d\n", rc);
- SQLGetDiagRec (SQL_HANDLE_ENV, *henv, 1, state,
- &dbms_err, err_msg,
- SQL_MAX_MESSAGE_LENGTH - 1, &length);
- printf ("DBMS error code:%d, SQL state: %s, message: %s\n",
- dbms_err, state, err_msg);
- exit (1);
- }
-
- rc = SQLAllocHandle (SQL_HANDLE_DBC, *henv, hdbc);
- if (rc != SQL_SUCCESS)
- {
- printf ("SQLAllocConnect failed with %d\n", rc);
- SQLGetDiagRec (SQL_HANDLE_ENV, *henv, 1, state,
- &dbms_err, err_msg,
- SQL_MAX_MESSAGE_LENGTH - 1, &length);
- printf ("DBMS error code:%d, SQL state: %s, message: %s\n",
- dbms_err, state, err_msg);
- exit (1);
- }
-
- rc = SQLConnect (*hdbc, datasource, SQL_NTS,
- (SQLCHAR *) "", SQL_NTS,
- (SQLCHAR *) "", SQL_NTS);
- returncode_check (*hdbc, (SQLHSTMT)NULL, rc, "SQLConnect");
- return;
- }
- void returncode_check (
- SQLHDBC hdbc,
- SQLHSTMT hstmt,
- int rc,
- char *str
- )
- {
- SDWORD dbms_err = 0;
- SWORD length;
- unsigned char err_msg[SQL_MAX_MESSAGE_LENGTH];
- unsigned char state[6];
- char rc_str[80];
- if (rc != SQL_SUCCESS)
- {
-
-
- rc_to_str (rc, rc_str);
-
- if (hdbc != NULL)
- SQLGetDiagRec (SQL_HANDLE_DBC, hdbc, 1, state, &dbms_err,
- err_msg, SQL_MAX_MESSAGE_LENGTH - 1, &length);
- else
- SQLGetDiagRec (SQL_HANDLE_STMT, hstmt, 1, state, &dbms_err,
- err_msg, SQL_MAX_MESSAGE_LENGTH - 1, &length);
- printf ("%s ERROR (%s): DBMS code:%d, SQL state: %s, message: \n%s\n",
- str, rc_str, dbms_err, state, err_msg);
- if (rc != SQL_SUCCESS_WITH_INFO)
- {
-
- if (NULL != hstmt)
- {
-
- SQLFreeStmt (hstmt, SQL_DROP);
-
-
- }
- SQLDisconnect (hdbc);
- SQLFreeConnect (hdbc);
- exit (1);
- }
- }
- }
- static void rc_to_str (
- int rc,
- char *str
- )
- {
-
- switch (rc)
- {
- case SQL_SUCCESS:
- strcpy (str, "SQL_SUCCESS");
- break;
- case SQL_SUCCESS_WITH_INFO:
- strcpy (str, "SQL_SUCCESS_WITH_INFO");
- break;
- case SQL_NO_DATA_FOUND:
- strcpy (str, "SQL_NO_DATA_FOUND");
- break;
- case SQL_NEED_DATA:
- strcpy (str, "SQL_NEED_DATA");
- break;
- case SQL_STILL_EXECUTING:
- strcpy (str, "SQL_STILL_EXECUTING");
- break;
- case SQL_ERROR:
- strcpy (str, "SQL_ERROR");
- break;
- case SQL_INVALID_HANDLE:
- strcpy (str, "SQL_INVALID_HANDLE");
- break;
- default:
- strcpy (str, "UNKNOWN RETURN CODE!!");
- break;
- }
- }
|