#include "MyBCP.h" #include "odbcss.h" //1,Allocate an environment handle and a connection handle. //2,Set SQL_COPT_SS_BCP and SQL_BHCP_ON to enable bulk copy operations. void CMyBCP::Initialize() { SQLRETURN l_uiReturn; l_uiReturn=SQLAllocHandle(SQL_HANDLE_ENV,NULL,&m_hEnvironment); if( l_uiReturn!=SQL_SUCCESS && l_uiReturn!=SQL_SUCCESS_WITH_INFO ) return; l_uiReturn=SQLSetEnvAttr(m_hEnvironment,SQL_ATTR_ODBC_VERSION,(SQLPOINTER)SQL_OV_ODBC3,SQL_IS_INTEGER); if( l_uiReturn!=SQL_SUCCESS && l_uiReturn!=SQL_SUCCESS_WITH_INFO ) return; l_uiReturn=SQLAllocHandle(SQL_HANDLE_DBC,m_hEnvironment,&m_hConnection); if( l_uiReturn!=SQL_SUCCESS && l_uiReturn!=SQL_SUCCESS_WITH_INFO ) return; l_uiReturn=SQLSetConnectAttr(m_hConnection,SQL_COPT_SS_BCP,(void *)SQL_BCP_ON,SQL_IS_INTEGER); if( l_uiReturn!=SQL_SUCCESS && l_uiReturn!=SQL_SUCCESS_WITH_INFO ) return; }; //3,Connect to SQL Server void CMyBCP::ConnectToDB() { std::string dsn("DNS-MMLRESOLVE"); std::string user("sa"); std::string password("huawei123,"); SQLRETURN l_uiReturn; l_uiReturn=SQLConnect(m_hConnection, (UCHAR*)dsn.c_str(),SQL_NTS, (UCHAR*)user.c_str(),SQL_NTS, (UCHAR*)password.c_str(),SQL_NTS ); }; //4,Call bcp_init to set the following information: // .The name of the table or view to bulk copy from or to. // .Specify NULL for the name of the data file. // .The name of an data file to receive any bulk copy error messages(specify NULL // if you do not want a message file). // .The direction of the copy: DB_IN from the application to the view or table or // DB_OUT to the application from the table or view. void CMyBCP::call_bcp_init(std::string & tablename) { SQLRETURN l_uiReturn=bcp_init(m_hConnection,tablename.c_str(),NULL,NULL,DB_IN); }; //5,Call bcp_bind for each column in the bulk copy to bind the column to a program variable void CMyBCP::call_bcp_bind_char_field(char* pBlock, int colIndex) { RETCODE l_uiRETCODE=bcp_bind(m_hConnection,(LPCBYTE)pBlock, 0, SQL_VARLEN_DATA, (LPCBYTE)"