#include "stdafx.h"
#include <stdio.h>
#include <iostream.h>
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
void HandleError(SQLHANDLE hHandle, SQLSMALLINT hType, RETCODE RetCode)
{
SQLSMALLINT iRec = 0;
SQLINTEGER iError;
TCHAR szMessage[1000];
TCHAR szState[SQL_SQLSTATE_SIZE];
if (RetCode == SQL_INVALID_HANDLE)
{
fprintf(stderr,"Invalid handle!\n");
return;
}
while (SQLGetDiagRec(hType,
hHandle,
++iRec,
(SQLCHAR *)szState,
&iError,
(SQLCHAR *)szMessage,
(SQLSMALLINT)(sizeof(szMessage) /
sizeof(TCHAR)),
(SQLSMALLINT *)NULL) == SQL_SUCCESS)
{
fprintf(stderr,TEXT("[%5.5s] %s (%d)\n"),szState,szMessage,iError);
}
}
class ODBC_Class
{
//attributes
public:
SQLHANDLE henv;
SQLHANDLE hdbc1;
SQLHANDLE hstmt;
SQLRETURN rc;
//methods
public:
ODBC_Class();
~ODBC_Class();
SQLRETURN SendLongData();
};
ODBC_Class::ODBC_Class()
{
rc = SQL_SUCCESS; //init the return code variable
//Allocate an environment handle.
rc = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv); //Allocate the env handle.
//Set the ODBC application version to 3.x.
if (rc ==SQL_SUCCESS)
rc = SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION, (SQLPOINTER)
SQL_OV_ODBC3, SQL_IS_UINTEGER);
//Allocate a connection handle.
if (rc == SQL_SUCCESS)
rc = SQLAllocHandle(SQL_HANDLE_DBC,henv, &hdbc1);
}
ODBC_Class::~ODBC_Class()
{
//Free the connection handle.
if (hdbc1 != NULL)
SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);
//Free the environment handle.
if (henv != NULL)
SQLFreeHandle(SQL_HANDLE_ENV, henv);
}
int main(int argc, char* argv[])
{
SQLRETURN rc = SQL_SUCCESS;
CHAR DBName[100] = "YourDataBaseName";
CHAR User[100] = "YourUID";
CHAR Password[100] = "YourPassword";
CHAR SQLstmt[255];
CHAR* m_id = ("1234");
CHAR* m_name;
OLECHAR* pSrc;
SQLINTEGER bufsize = 204801;
SQLINTEGER ValSizeName = 5;
SQLINTEGER ValSizeID = SQL_DATA_AT_EXEC;
SQLINTEGER paramMarker = 1;
ODBC_Class Example; //Create an instance of the ODBC_Class to use.
if (Example.hdbc1 != NULL)
{
//Connect to the database.
rc = SQLConnect(Example.hdbc1, (SQLCHAR*) DBName, SQL_NTS,
(SQLCHAR*)User, SQL_NTS, (SQLCHAR*) Password,
SQL_NTS);
if (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
cout << "Connected to " << DBName<< " \n\n" << endl;
//Allocate stmt handle.
rc = SQLAllocStmt(Example.hdbc1, &Example.hstmt);
if ( rc == SQL_SUCCESS)
{
UINT info2 = 60; //Set the timeout value in seconds.
//Fill in the data "abcd...."
m_name = new char[bufsize];
for (int i = 0; i < bufsize; i++)
m_name[i] = (i % 26) + 65;
m_name[bufsize-1] = '#';
OLECHAR* m_olechar = new OLECHAR[bufsize*2];
int result = MultiByteToWideChar(CP_ACP, 0, m_name, -1,
m_olechar, bufsize*2);
//Bind the parameters.
rc = SQLBindParameter(Example.hstmt,1,SQL_PARAM_INPUT,
SQL_C_WCHAR, SQL_LONGVARCHAR,
bufsize*2, 0, (SQLPOINTER)
paramMarker, 0, &ValSizeName);
if (rc != SQL_SUCCESS)
{
HandleError(Example.hstmt,SQL_HANDLE_STMT, rc);
}
rc = SQLBindParameter(Example.hstmt,2,SQL_PARAM_INPUT,
SQL_C_DEFAULT, SQL_CHAR, 4, 0, (SQLPOINTER) m_id, 4, NULL);
strcpy(SQLstmt,"UPDATE repro SET name=? WHERE id=?");
ValSizeName = SQL_LEN_DATA_AT_EXEC(bufsize*2);
rc = SQLExecDirect(Example.hstmt, (SQLCHAR*) SQLstmt,
SQL_NTS);
paramMarker = -1;
rc = SQLParamData(Example.hstmt,
(SQLPOINTER*)¶mMarker);
//Loop through and insert the data.
int j = 2*bufsize/8188;
int remains = bufsize*2;
pSrc = m_olechar;
for (int k=0; k<j; k++)
{
remains -= 8188;
rc =SQLPutData(Example.hstmt,(SQLPOINTER)pSrc , 8188);
if (rc != SQL_SUCCESS)
HandleError(Example.hstmt,SQL_HANDLE_STMT, rc);
pSrc += 4094; //Increment widechar pointer by 4094 = 8188
//bytes
cout << rc << "\n";
}
rc =SQLPutData(Example.hstmt,(SQLPOINTER)pSrc , remains);
rc = SQLParamData(Example.hstmt, (SQLPOINTER*)paramMarker);
if (rc != SQL_SUCCESS)
HandleError(Example.hstmt, SQL_HANDLE_STMT, rc);
}
rc = SQLDisconnect(Example.hdbc1);
}
return (rc);
}