#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <iostream>
using namespace std;
#define DEBUG // Debug Version
SQLHENV V_OD_Env; // Handle ODBC environment
long V_OD_erg; // result of functions
SQLHDBC V_OD_hdbc; // Handle connection
SQLCHAR V_OD_stat[10]; // Status SQL
SQLINTEGER V_OD_err,V_OD_rowanz,V_OD_id;
SQLSMALLINT V_OD_mlen;
char V_OD_buffer[200];
SQLCHAR V_OD_msg[200];
#define DBINT long
#define DBCHAR char
#define DBSMALLINT short
#define MAX_VAR_CHAR_LEN 255
#define EVENT_TYPE_LEN 6
typedef struct {
DBINT RID, objectID, FSEventID, OIDRelation, MiscID;
DBCHAR eventType[EVENT_TYPE_LEN+1], status;
DBSMALLINT persistent;
DBINT start, end, oldStart, oldEnd, scopeIDBitMask;
DBCHAR * data; //NULL or pointer to MAX of 256 character.
} SNST_MsgQueue;
static SNST_MsgQueue SNSG_event;
static char SNSG_userDataBind[MAX_VAR_CHAR_LEN+1];
//#define CONNSTR "DRIVER={SQL Server};SERVER=dev05;UID=sa;PWD=sa;DATABASE=sca;"
#define CONNSTR "ODBC;UID=mag_db_setup;PWD=whoknows;DRIVER={Oracle in OraHome90};DBQ=scaDev05_dev20;"
#define CONNLEN (sizeof(CONNSTR) - 1)
#ifdef DEBUG
extern void OD_ListDSN(void);
#endif
void main()
{
cout << "Begin...: " << endl;
SQLHSTMT V_OD_hstmt; // Handle for querying
SQLINTEGER V_OD_id; // Variable for table column element
SQLINTEGER V_OD_sex; // Variable for table column element
SQLINTEGER V_OD_ret; // Variable for table column element
SQLINTEGER V_OD_SequenceRID; // Variable for table column element
SQLCHAR V_OD_CrewCode[10]; // Variable for table column element
SQLINTEGER nSeqNum;
SQLCHAR V_OD_query[] = "SELECT RID, CrewCode FROM crew_member";
SQLSMALLINT V_OD_colanz; // Get number of result columns
SQLINTEGER V_OD_rowanz; // Get number of result rows
SQLCHAR szConnStr[255]; // Get output connection string
SWORD cbConnStr; // Get output number of connection string
//
// 1. allocate Environment handle and register version
//
V_OD_erg=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&V_OD_Env);
if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))
{
printf("Error AllocHandle\n");
exit(0);
}
V_OD_erg=SQLSetEnvAttr(V_OD_Env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))
{
printf("Error SetEnv\n");
SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);
exit(0);
}
//
// 2. allocate connection handle, set timeout
//
V_OD_erg = SQLAllocHandle(SQL_HANDLE_DBC, V_OD_Env, &V_OD_hdbc);
if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))
{
printf("Error AllocHDB %d\n",V_OD_erg);
SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);
exit(0);
}
SQLSetConnectAttr(V_OD_hdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)5, 0);
//
// 3. Connect to the datasource "rorot_dsn"
//
//V_OD_erg = SQLConnect(V_OD_hdbc, (SQLCHAR*) "sca", SQL_NTS,
// (SQLCHAR*) "sa", SQL_NTS, (SQLCHAR*) "sa", SQL_NTS);
V_OD_erg = SQLDriverConnect(V_OD_hdbc, NULL, (UCHAR*)CONNSTR, CONNLEN,
szConnStr, sizeof(szConnStr), &cbConnStr, SQL_DRIVER_NOPROMPT);
#ifdef DEBUG
// Display current DSN information
OD_ListDSN();
#endif
if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))
{
printf("Error SQLConnect %d\n",V_OD_erg);
SQLGetDiagRec(SQL_HANDLE_DBC, V_OD_hdbc, 1, &V_OD_stat[0],
&V_OD_err, &V_OD_msg[0], 100, &V_OD_mlen);
printf("%s (%d)\n",V_OD_msg,V_OD_err);
SQLFreeHandle(SQL_HANDLE_DBC, V_OD_hdbc);
SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);
exit(0);
}
#ifdef DEBUG
printf("Connected!\n");
#endif
//
// 4. Query information from database' table
//
char szStatement[1024];
strcpy(szStatement, "{? = call sp_NTF_msgQueuePut('CLNT01', 0, 0, 0, 0, 0)} ");
V_OD_erg = SQLAllocHandle(SQL_HANDLE_STMT, V_OD_hdbc, &V_OD_hstmt);
nSeqNum = 0;
long iLen = sizeof(nSeqNum);
SQLBindParameter(V_OD_hstmt,1,SQL_PARAM_OUTPUT,SQL_C_LONG,
SQL_INTEGER, 4, 0, &nSeqNum,
sizeof(SQL_C_LONG), &iLen);
V_OD_erg = SQLExecDirect(V_OD_hstmt, (UCHAR*)szStatement, SQL_NTS);
printf("{? = call sp_NTF_msgQueuePut('CLNT01', 0, 0, 0, 0, 0)}->MaxNumber:%d\n", nSeqNum);
//
// 5. Disconnect the datasource "rorot_dsn"
//
SQLFreeHandle(SQL_HANDLE_STMT,V_OD_hstmt);
SQLDisconnect(V_OD_hdbc);
SQLFreeHandle(SQL_HANDLE_DBC,V_OD_hdbc);
SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);
#ifdef DEBUG
printf ( "Disconnect safely!\n" );
#endif
cout << "End!" << endl;
return;
}
#ifdef DEBUG
void OD_ListDSN(void)
{
SQLCHAR l_dsn[100], l_desc[100];
short int l_len1, l_len2, l_next;
l_next = SQL_FETCH_FIRST;
while ( SQLDataSources(V_OD_Env, l_next, l_dsn, sizeof(l_dsn),
&l_len1, l_desc, sizeof(l_desc), &l_len2) == SQL_SUCCESS )
{
printf( "Server=(%s) Beschreibung=(%s)\n", l_dsn, l_desc);
l_next = SQL_FETCH_NEXT;
}
}
#endif
评论2
最新资源