CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VC/MFC >  基础类

各位ADO编程高手,请教:

楼主napoleonlzh(搜索客)2003-09-12 16:52:24 在 VC/MFC / 基础类 提问

vc中读取SQL   Server中的text类型的字段内容,采取什么方法最好?  
        我试过GetChunk,   AppendChunk   方法,可是都不行,text类型的字段中写入.jpg,   .rar文件,   用textcopy读出来时是正确的,可是vc读就没辙了,各位高手有高招否? 问题点数:100、回复次数:8Top

1 楼masterz(www.fruitfruit.com)回复于 2003-09-12 17:03:56 得分 90

主  题:     怎么样从数据库中取出二进制数据到ADO的Stream对象中来..    
  #import   "c:\program   files\common   files\system\ado\msado15.dll"   no_namespace   rename("EOF","adoEOF")  
  int   main(int   argc,   char*   argv[])  
  {  
  printf("This   sample   shows   you   how   to   access   BLOB   via   ADO   and   store   it   as   file"  
  "The   second   field   of   the   database   is   BLOB\n"  
  "If   it   works,   it   is   writen   by   masterz,\n"  
  "otherwise   I   don't   know   who   write   it\n");  
  CoInitialize(NULL);  
  try  
  {  
  _ConnectionPtr   pConn("ADODB.Connection");  
  _RecordsetPtr     pRst("ADODB.Recordset");  
  _variant_t   varBLOB;  
  pConn->Open(_bstr_t("Driver={Microsoft   Access   Driver   (*.mdb)};DBQ=GetChunk.mdb"),"","",adConnectUnspecified);  
  pRst->Open(_bstr_t("BlobTable"),_variant_t((IDispatch   *)   pConn,   true),  
  adOpenKeyset,   adLockOptimistic,   adCmdTable);  
  pRst->MoveFirst();  
  //long   lDataLength   =   pRst->Fields->Item[1L]->ActualSize;  
  //varBLOB   =   pRst->Fields->Item[1L]->GetChunk(lDataLength);  
  _StreamPtr   stream;  
  stream.CreateInstance("ADODB.Stream");  
  _variant_t   varOptional(DISP_E_PARAMNOTFOUND,VT_ERROR);    
  stream->raw_Open(varOptional,   adModeUnknown,   adOpenStreamUnspecified,NULL,NULL);  
  stream->put_Type(adTypeBinary);  
  stream->Write(pRst->GetFields()->GetItem(_variant_t((long)1))->GetValue());  
  stream->SaveToFile("test.txt",adSaveCreateOverWrite);  
  printf("save   stream   to   file   finished\n");  
  stream->Close();  
  pRst->Close();  
  pConn->Close();  
  }  
  catch   (_com_error   &e)  
  {  
  printf("Description   =   '%s'\n",   (char*)   e.Description());  
  }  
  ::CoUninitialize();  
  return   0;  
  }  
  Top

2 楼masterz(www.fruitfruit.com)回复于 2003-09-12 17:04:41 得分 5

///////////////////////////////////////////////////////////////////  
  //read   "TEXT"   field   of   SQL   server   table  
  #include   "stdafx.h"  
  #import   "C:\PROGRA~1\COMMON~1\System\ado\msado15.dll"   rename(   "EOF",   "adoEOF"   )  
      struct   InitOle  
      {  
          InitOle()     {   ::CoInitialize(NULL);   }  
          ~InitOle()   {   ::CoUninitialize();     }  
      }   _init_InitOle_;  
  void   PrintProviderError(ADODB::_ConnectionPtr   pConnection);  
   
  int   main(int   argc,   char*   argv[])  
  {  
  printf("read   text   field   from   SQL   server\n   by   masterz\n");  
  ADODB::_ConnectionPtr     Conn1;  
  ADODB::_RecordsetPtr     Rs1;  
  _variant_t     vtEmpty   (DISP_E_PARAMNOTFOUND,   VT_ERROR);  
  _variant_t     vtEmpty2   (DISP_E_PARAMNOTFOUND,   VT_ERROR);  
  _bstr_t         bstrConnect(   L"driver={sql   server};server=Cell;Database=zhg;UID=sa;PWD=;"   );  
  try  
  {  
  _bstr_t   bstrEmpty;  
  Conn1.CreateInstance(   __uuidof(   ADODB::Connection   )   );  
  Rs1.CreateInstance(__uuidof(ADODB::Recordset));  
  //   Establish   connection.  
  Conn1->ConnectionString   =   bstrConnect;  
  Conn1->Open(   bstrConnect,   bstrEmpty,   bstrEmpty,   -1   );  
  Rs1->put_CursorLocation(ADODB::adUseClient);  
  Rs1->Open("TABLE3",   _variant_t((IDispatch   *)   Conn1,   true),ADODB::adOpenStatic,   ADODB::adLockReadOnly,   ADODB::adCmdTable);  
  long   ltextfield_index=3L;  
  long   lDataLength   =   Rs1->Fields->Item[ltextfield_index]->ActualSize;  
  if(lDataLength)  
  {  
  VARIANT   varBLOB;  
  VariantInit(&varBLOB);  
  //   Get   the   chunk   of   data   in   the   second   field.  
  varBLOB   =   Rs1->Fields->Item[ltextfield_index]->GetChunk(lDataLength);  
  if(varBLOB.vt==VT_BSTR)  
  {  
  printf("Text   field   length:%d,valuse:%s\n",lDataLength,(LPCTSTR)_bstr_t(varBLOB.bstrVal));  
  }  
  else  
  printf("datalength:%d,   not   bstr   value\n",lDataLength);  
  }  
  Rs1->Close();  
  Conn1->Close();  
  }  
  catch(_com_error   &e)  
  {  
  _bstr_t   bstrSource(e.Source());  
  _bstr_t   bstrDescription(e.Description());  
  printf("\nCOM   error   occurred,   Source   :   %s   \n   Description   :   %s   \n",(LPCSTR)bstrSource,(LPCSTR)bstrDescription);  
  PrintProviderError(Conn1);  
  }  
  return   0;  
  }  
  VOID   PrintProviderError(ADODB::_ConnectionPtr   pConnection)  
  {  
      //   Print   Provider   Errors   from   Connection   object.  
      //   pErr   is   a   record   object   in   the   Connection's   Error   collection.  
                  ADODB::ErrorPtr     pErr   =   NULL;  
      long             nCount   =   0;  
      long             i   =   0;  
   
      if(   (pConnection->Errors->Count)   >   0)  
      {  
              nCount   =   pConnection->Errors->Count;  
              //   Collection   ranges   from   0   to   nCount   -1.  
              for(i   =   0;   i   <   nCount;   i++)  
              {  
                  pErr   =   pConnection->Errors->GetItem(i);  
                  printf("\n\t   Error   number:   %x\t%s",   pErr->Number,   (LPCSTR)pErr->Description);  
              }  
      }  
  }  
  Top

3 楼bluebohe(薄荷)回复于 2003-09-12 17:05:30 得分 5

vc   ADO读出来的text字段的var是_bstr_t类型的数据,里面存放的字符可能是UNICODE型的,所以你要准确的计算到地读出多少内容不大方便,但也不是没有办法,你可以把它强制转换成(LPCTSTR)后处理  
  比如m_pRecordSet->Fields->Item[strTabName]->ActualSize;   里面存放的是字符的长度  
  比如“a中国”长度是5,但是GetChunk(3)就能完全把他们取出来了  
  Top

4 楼wangjinwang(王者之疯)回复于 2003-09-12 17:13:16 得分 0

既然存储二进制文件,为什么不定义IMAGE类型字段呢。Top

5 楼yyfa6(yyfa6)回复于 2003-09-12 17:19:02 得分 0

高手们!Top

6 楼nirovf(司马缸-砸光 之 乡村程序员)回复于 2003-09-14 10:42:31 得分 0

正在学习中Top

7 楼shanjicn(有容乃大)回复于 2003-09-14 10:44:39 得分 0

学习Top

8 楼chen_pin(小品)回复于 2003-09-14 11:15:35 得分 0

UP   ItTop

相关问题

  • 请教各位ado编程菜鸟问题
  • 各位请推荐一本介绍ADO编程方面的书
  • 向各位大虾求助,ado数据库编程源码。
  • 有关ADO编程
  • 奇怪的ADO?,请各位爱好编程的朋友讨论,散分!
  • 奇怪的ADO?,请各位爱好编程的朋友讨论,散分!
  • ADO编程问题求解
  • ADO编程(初学者)
  • ADO 编程资料查询
  • [讨论]关于ADO控件编程和ADO对象编程

关键词

  • 字段
  • ado
  • vc
  • prst
  • getchunk
  • pconn
  • 类型
  • bstr
  • fields
  • item

得分解答快速导航

  • 帖主:napoleonlzh
  • masterz
  • masterz
  • bluebohe

相关链接

  • Visual C++类图书
  • Visual C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo