#include <windows.h>
#include <stdio.h>
#include <iostream.h>
#include <sql.h>
#include <sqlext.h>
SQLHANDLE hEnv;
SQLHANDLE hConn;
SQLHANDLE hStmt;
void ShowErrors(HENV, HDBC, HSTMT);
void main(void)
{
RETCODE rc;
SQLCHAR Statement[255];
strcpy((char *) Statement, "INSERT INTO dates (d) VALUES (?)");
SQLCHAR StatementCreateTable[255];
strcpy((char *) StatementCreateTable, "create table DATES (d DATE)");
SQLCHAR dsn[255];
strcpy((char *) dsn,"OracleServer");
SQLCHAR user[255];
strcpy((char *) user,"scott");
SQLCHAR pass[255];
strcpy((char *) pass,"tiger");
// January 3, 500 B.C.
SQL_TIMESTAMP_STRUCT value;
value.hour = 0;
value.minute = 0;
value.second = 0;
value.year = -499;
value.month = 1;
value.day = 3;
value.fraction = 0;
rc = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv);
if (rc == SQL_SUCCESS)
{
rc = SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION,
(SQLPOINTER) SQL_OV_ODBC3, SQL_IS_UINTEGER);
}
if (rc == SQL_SUCCESS)
{
rc = SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hConn);
}
rc = SQLConnect(hConn, dsn, SQL_NTS, (SQLCHAR *) user, SQL_NTS, (SQLCHAR *) pass, SQL_NTS);
rc = SQLAllocHandle(SQL_HANDLE_STMT,hConn,&hStmt);
//create the Dates table
rc = SQLExecDirect(hStmt, StatementCreateTable, SQL_NTS);
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)
ShowErrors(hEnv, hConn, hStmt);
SQLFreeStmt(hStmt, SQL_CLOSE);
//Bind the SQL_TIMESTAMP_STRUCT as the input parameter
rc = SQLBindParameter(hStmt, 1, SQL_PARAM_INPUT, SQL_C_TYPE_TIMESTAMP, SQL_TYPE_TIMESTAMP, 23, 3,
(SQLPOINTER) &value, 0, NULL);
//Execute the INSERT statement
rc = SQLExecDirect(hStmt, Statement, SQL_NTS);
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)
ShowErrors(hEnv, hConn, hStmt);
SQLFreeStmt(hStmt, SQL_CLOSE);
SQLFreeHandle(SQL_HANDLE_STMT, hStmt);
SQLDisconnect(hConn);
SQLFreeHandle(SQL_HANDLE_DBC, hConn);
}
void ShowErrors (HENV henv, HDBC hdbc, HSTMT hstmt)
{
//Variables for SQLGetDiagRec
SQLCHAR sqlState[20];
SQLCHAR errorMsg[1000];
SQLSMALLINT errorMsgLen;
SQLINTEGER nativeError;
char szTemp[4096];
//Retrieve the Raised error message
SQLGetDiagRec(SQL_HANDLE_STMT,hstmt,1,sqlState,
&nativeError,errorMsg,1000,&errorMsgLen);
//Display the size of the returned error message, and the message itself
sprintf(szTemp, "Length=[%d] Text=%s", strlen((char*)errorMsg), errorMsg );
printf("ERROR! %s\n\n\n", szTemp);
printf("Press any key...");
getchar();
SQLFreeStmt(hStmt, SQL_CLOSE);
SQLFreeHandle(SQL_HANDLE_STMT, hStmt);
SQLDisconnect(hConn);
SQLFreeHandle(SQL_HANDLE_DBC, hConn);
exit(0);
}