博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
bcp功能
阅读量:4652 次
发布时间:2019-06-09

本文共 5872 字,大约阅读时间需要 19 分钟。

#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)"\0", sizeof(WCHAR), SQLCHARACTER, colIndex);     if(l_uiRETCODE==SUCCEED)     {         printf("call_bcp_bind_char_field() ok!\n");     } }; void CMyBCP::call_bcp_bind_int_field(int &iBlock, int colIndex) {     RETCODE l_uiRETCODE=bcp_bind(m_hConnection,(BYTE *)&iBlock, 0, sizeof(DBINT), NULL, (INT)NULL, SQLINT4, colIndex);     if(l_uiRETCODE==SUCCEED)     {         printf("call_bcp_bind_int_field() ok!\n");     } };   //6,Fill the program variables with data,and call bcp_sendrow to send a row of data. void CMyBCP::call_bcp_sendrow() {     RETCODE l_uiRETCODE=bcp_sendrow(m_hConnection);     if(l_uiRETCODE==SUCCEED)     {         printf("bcp_sendrow() ok!");     } };   //7,After several rows have been sent,call bcp_batch to checkpoint the rows already sent. //It is good practice to call bcp_batch at least once per 1000 rows. void CMyBCP::call_bcp_batch() {     DBINT l_uiDBINT=bcp_batch(m_hConnection); };   //8,After all rows have been sent,call bcp_done to complete the operation. void CMyBCP::call_bcp_done() {     DBINT l_uiDBINT=bcp_done(m_hConnection); };     CMyBCP::CMyBCP() {     Initialize();     ConnectToDB(); };   CMyBCP::~CMyBCP() {     if(m_hConnection!=SQL_NULL_HDBC)     {         SQLDisconnect(m_hConnection);         SQLFreeHandle(SQL_HANDLE_DBC, m_hConnection);     }     if(m_hEnvironment!=SQL_NULL_HENV)     {         SQLFreeHandle(SQL_HANDLE_ENV,m_hEnvironment);     } };

 

 

#include 
#include
#include
#pragma comment(lib,"odbc32.lib") #pragma comment(lib,"odbcbcp.lib") #pragma comment(lib,"odbcbcp.lib") class CMyBCP { public: CMyBCP(); ~CMyBCP(); public: //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 Initialize(); //3,Connect to SQL Server void ConnectToDB(); //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 call_bcp_init(std::string & tablename); //5,Call bcp_bind for each column in the bulk copy to bind the column to a program variable //void call_bcp_bind(); void call_bcp_bind_char_field(char* pBlock, int colIndex); void call_bcp_bind_int_field(int &iBlock, int colIndex); //6,Fill the program variables with data,and call bcp_sendrow to send a row of data. void call_bcp_sendrow(); //7,After several rows have been sent,call bcp_batch to checkpoint the rows already sent. //It is good practice to call bcp_batch at least once per 1000 rows. void call_bcp_batch(); //8,After all rows have been sent,call bcp_done to complete the operation. void call_bcp_done(); private: HENV m_hEnvironment; HDBC m_hConnection; };

 

 

#include <iostream>
#include "MyBCP.h"
int main()
{
CMyBCP bcp;
std::string tablename("[p].[e_LOG_IOEXP]");
bcp.call_bcp_init(tablename);
int int_BSCFlg;
int int_ObjFlg;
std::string str_BSCNAME("");
std::string str_IDENTITY("BXB001A");
bcp.call_bcp_bind_int_field(int_BSCFlg,1);
bcp.call_bcp_bind_int_field(int_ObjFlg,2);
bcp.call_bcp_bind_char_field(const_cast<char *>(str_BSCNAME.c_str()),3);
bcp.call_bcp_bind_char_field(const_cast<char *>(str_IDENTITY.c_str()),4);
bcp.call_bcp_sendrow();
//bcp.call_bcp_batch();
bcp.call_bcp_done();
std::cout<<"Over"<<std::endl;
getchar();
return 0;
};

转载于:https://www.cnblogs.com/hongjiumu/p/3462122.html

你可能感兴趣的文章
[javascript]9宫格拖拽拼图游戏 puzzle
查看>>
Entity Framework底层操作封装(3)
查看>>
InputStream 转换 InputStreamReader再转换BufferedReader
查看>>
在线程池中的使用spring aop事务增强
查看>>
继续深入了解Cookie 和 Session
查看>>
再看《操作系统》--处理机管理
查看>>
亚马逊的负载均衡(Amazon Load Balancing)
查看>>
Java学习之Comparable与Comparator的区别
查看>>
ASP:Checkbox验证非空的一种方法
查看>>
[CQOI2013]新Nim游戏 线性基
查看>>
我的成就故事
查看>>
VB6.0 API 累计
查看>>
第十周学习进度博客
查看>>
Ecshop 最小起订量如何设置
查看>>
不使用其他变量实现两变量互换
查看>>
bcp功能
查看>>
xcode项目打不开:incompatible project version问题
查看>>
学习网站
查看>>
C#4.0新特性dynamic\可选参数、命名参数
查看>>
使用免费SSL证书让网站支持HTTPS访问
查看>>