VC++ 调用BCP 导出 Sqlserver 表

 首先准备一条bcp语句,测试一下:
EXEC master..xp_cmdshell 'bcp myTest.dbo.firest out d:/tmp.csv /c /t /S /U sa /P sa'
这句话导出myTest数据库中的firest表到物理d盘下的tmp.csv文件中,在TSQL环境下测试成功
那么在VC++下呢,首先连接数据库,执行该语句,源码如下:
  1. // DBTransaction.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "shlobj.h"
  5. #include "ImportExport.h"
  6. #include "DBTransaction.h"
  7. // DBTransaction
  8. IMPLEMENT_DYNAMIC(DBTransaction, CWnd)
  9. DBTransaction::DBTransaction()
  10. {
  11.     AfxOleInit();
  12.     try
  13.     {
  14.         m_hrs = m_conn.CreateInstance("ADODB.Connection");
  15.         if(m_hrs == S_OK)
  16.         {
  17.             m_hrs = m_conn->Open("driver={SQL Server};Server=.;DATABASE=master;UID=sa;PWD=sa","","",adModeUnknown);
  18.             //this->MessageBox(L"数据库连接成功");
  19.         }
  20.     }
  21.     catch(_com_error e)
  22.     {
  23.         CString msg;
  24.         msg.Format(L"数据库连接失败,/n错误信息:%s",e.ErrorMessage());
  25.         this->MessageBox(msg);
  26.     }
  27. }
  28. DBTransaction::DBTransaction(CString IP,CString DB,CString UID,CString PWD)
  29. {
  30. //  AfxOleInit();
  31.     CString _IP(IP);
  32.     CString _DB(DB);
  33.     CString _UID(UID);
  34.     CString _PWD(PWD);
  35.     CString connstr = L"driver={SQL Server};Server=" + _IP + ";DATABASE=" + _DB + ";UID=" + _UID + ";PWD="+_PWD;
  36.     try
  37.     {
  38.         m_hrs = m_conn.CreateInstance("ADODB.Connection");
  39.         if(m_hrs == S_OK)
  40.         {
  41.             m_hrs = m_conn->Open(_bstr_t(connstr),"","",adModeUnknown);
  42.             //this->MessageBox(L"数据库连接成功");
  43.         }
  44.     }
  45.     catch(_com_error e)
  46.     {
  47.         CString msg;
  48.         msg.Format(L"数据库连接失败,/n错误信息:%s",e.ErrorMessage());
  49.         this->MessageBox(msg);
  50.     }
  51. }
  52. HRESULT DBTransaction::TraceConn()
  53. {
  54.     return this->m_hrs;
  55. }
  56. void DBTransaction::GetDataBases()
  57. {
  58.     variant_t var;
  59.     CString sql("select name from sysdatabases");
  60.     _RecordsetPtr set;
  61.     set.CreateInstance("ADODB.Recordset");
  62.     set->Open((_variant_t)sql,_variant_t((IDispatch *)this->m_conn,true),adOpenStatic,adLockOptimistic,adCmdText);
  63.     while(!set->EndOfFile)
  64.     {
  65.         var = (_bstr_t)set->GetCollect("name");
  66.         if(var.vt != VT_NULL)
  67.             this->databases.AddTail((LPCTSTR)_bstr_t(var));
  68.         set->MoveNext();
  69.     }
  70. }
  71. void DBTransaction::GetTables()
  72. {
  73.     CString empty(" ");
  74.     CString tablePropertis;
  75.     variant_t var;
  76.     CString sql("select name,(case when xtype='u' then '表' when xtype='v' then '视图' end) xtype,crdate from sysobjects where xtype='u' or xtype='v'");
  77.     _RecordsetPtr set;
  78.     set.CreateInstance("ADODB.Recordset");
  79.     set->Open((_variant_t)sql,_variant_t((IDispatch *)this->m_conn,true),adOpenStatic,adLockOptimistic,adCmdText);
  80.     while(!set->EndOfFile)
  81.     {
  82.         //循环读取数据库,并写入数组
  83.         tablePropertis.Empty();
  84.         var = (_bstr_t)set->GetCollect("name");
  85.         if(var.vt != VT_NULL)
  86.             tablePropertis += (LPCTSTR)_bstr_t(var);
  87.         var = (_bstr_t)set->GetCollect("crdate");
  88.         if(var.vt != VT_NULL)
  89.             tablePropertis += empty + (LPCTSTR)_bstr_t(var);
  90.         var = (_bstr_t)set->GetCollect("xtype");
  91.         if(var.vt != VT_NULL)
  92.             tablePropertis += empty + (LPCTSTR)_bstr_t(var);
  93.         tables.AddTail(tablePropertis);
  94.         set->MoveNext();
  95.     }
  96. }
  97. void DBTransaction::Exec(CString sql)
  98. {
  99.     _CommandPtr comm;
  100.     comm.CreateInstance("ADODB.Command");
  101.     comm->ActiveConnection = this->m_conn;
  102.     comm->CommandText = _bstr_t(sql);
  103.     //MessageBox(comm->CommandText);
  104.     comm->CommandType = adCmdText;
  105.     try
  106.     {
  107.         comm->Execute(NULL,NULL,adCmdUnknown);
  108.     }
  109.     catch(_com_error &e)
  110.     {
  111.         MessageBox(e.Description());
  112.     }
  113. }
  114. void DBTransaction::CloseDataBase()
  115. {
  116.     this->m_conn->Close();
  117. }
  118. DBTransaction::~DBTransaction()
  119. {
  120. }
  121. BEGIN_MESSAGE_MAP(DBTransaction, CWnd)
  122. END_MESSAGE_MAP()
  123. // DBTransaction 消息处理程序
Exec函数中就是执行导出语句的的方法,当然任何合法的sql语句都能执行


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值