#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, , 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, , 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 <Windows.h>
#include <sqlext.h>
#include <string> #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;
};

bcp功能的更多相关文章

  1. oracle数据库基础功能

    一.oracle基本常用的数据类型 varchar(长度) 字符串char(长度) 字符number(x,y) x表示总位数 y表示保留小数点后几位数 eg面试题:number(5,3)最大的数是99 ...

  2. 你搞懂 ORACLE、 SQLSERVER、MYSQL与DB2的区别了吗

    ORACLE. SQLSERVER.MYSQL与DB2的区别--平台性:    Oracle.MYSQL与DB2可在所有主流平台上运行:    SQL Server只能在Windows下运行: --安 ...

  3. c#数据批量插入

    由于之前面试中经常被问到有关EF的数据批量插入问题,今天以Sqlserver数据库为例,对.net中处理数据批量处理的方案进行了测试对比. 1.四种测试方案 (1)普通的EF数据批量插入:即调用DbS ...

  4. sqlserver中BCP命令导入导出

    个人自用导出文本文件命令: bcp [xxDB].[dbo].[xx_tb_name] out d:\temp\xxx.txt -c -t "\t" -T bcp是SQL Serv ...

  5. BCP导出导入大容量数据实践

    前言 SQL SERVER提供多种不同的数据导出导入的工具,也可以编写SQL脚本,使用存储过程,生成所需的数据文件,甚至可以生成包含SQL语句和数据的脚本文件.各有优缺点,以适用不同的需求.下面介绍大 ...

  6. BCP

    转:(总结) SQL Server Bulk Insert 批量数据导入 DB SQL ServerBulk InsertBCPOPENROWSET格式文件 SQL Server的Bulk Inser ...

  7. sql 2005,2008开启bcp的方法嗯哈步骤

    sqlserver 2008开启bcp服务的方法和步骤 sqlserver 2005开启bcp服务的方法和步骤 在开始菜单中找到sql server 2005 -->> 配置工具 --&g ...

  8. 数据库调优过程(一):SqlServer批量复制(bcp)[C#SqlBulkCopy]性能极低问题

    背景 最近一段给xx做项目,这边最头疼的事情就是数据库入库瓶颈问题. 环境 服务器环境:虚拟机,分配32CPU,磁盘1.4T,4T,5T,6T几台服务器不等同(转速都是7200r),内存64G. 排查 ...

  9. SSIS结合BCP及SQL Server作业实现定时将数据导出打包实现数据同步

    首先这个流程要实现的功能大致是: 有两台服务器,一台是对外网开发的,一台是内网的.那么很明显数据交互都是外网服务器在做,而这个流程要做的就是要将外网上面的数据定时同步到内网中. 我们依对其中某张表的操 ...

随机推荐

  1. java===java基础学习(6)---流程控制,for,if,switch,continue,break

    注意点: for循环的用法和python截然不同,注意格式 switch~,switch对应的case每当执行完毕都要break,由于基本不怎么用switch,所以作为了解. 中断流程控制语句,请考虑 ...

  2. monkey测试===ios-monkey测试工具

    iOSmonkey测试工具: crashmonkey 特点: 支持**真机测试.模拟器测试** 支持收集**系统日志(Systemlog)**.**崩溃日志(Crashlog)**.***instru ...

  3. 移动测试===利用adb命令查看apk文件包名的一些方法

    前提是已经下载android SDK并配好环境变量! 在控制台输入命令$adb shell pm 可以看到adb shell pm的相关用法,详细信息请自己看输出 要看一个apk文件的相关信息最简单实 ...

  4. 【总结】IE和Firefox的Javascript兼容性总结

    长久以来JavaScript兼容性一直是Web开发者的一个主要问题.在正式规范.事实标准以及各种实现之间的存在的差异让许多开发者日夜煎熬.为此,主要从以下几方面差异总结IE和Firefox的Javas ...

  5. python插入oracle数据

    # coding=utf- ''''' Created on -- @author: ''' import json; import urllib2 import sys import cx_Orac ...

  6. 取消myeclipse自动进入workspace

    进入到C:\Program Files\MyEclipse 6.5\eclipse\configuration\.settings目录, 修改SHOW_WORKSPACE_SELECTION_DIAL ...

  7. 【hdoj_2570】迷障

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2570 思路:贪心法.要求在浓度不超标的情况下,解药的最大体积.由于体积相同,可以先对浓度排序,然后从浓度小 ...

  8. 监测mysql错误日志,有错误自动邮件报警

    监测mysql错误日志,有错误自动邮件报警 http://blog.csdn.net/yabingshi_tech/article/details/51443401 MySQL:监控慢日志.错误日志. ...

  9. OpenStack 安装数据库和rabbitmq消息队列 (三)

    一)安装配置数据库 1.1.安装包 # yum install mariadb mariadb-server python2-PyMySQL -y 1.2.配置数据库 # vim /etc/my.cn ...

  10. 出现Unrecognized field "state" (class com.jt.manage.pojo.ItemCat)异常

    当在pojo中,往往会出现字段无法一一对应时,有可能就会出现创建Unrecognized field "state" (class com.jt.manage.pojo.ItemC ...