DemoBlobInsertDlg.cpp
上传用户:weisheen
上传日期:2022-07-09
资源大小:19390k
文件大小:8k
源码类别:

ActiveX/DCOM/ATL

开发平台:

Visual C++

  1. // DemoBlobInsertDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "DemoBlobInsert.h"
  5. #include "DemoBlobInsertDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CAboutDlg dialog used for App About
  13. class CAboutDlg : public CDialog
  14. {
  15. public:
  16. CAboutDlg();
  17. // Dialog Data
  18. //{{AFX_DATA(CAboutDlg)
  19. enum { IDD = IDD_ABOUTBOX };
  20. //}}AFX_DATA
  21. // ClassWizard generated virtual function overrides
  22. //{{AFX_VIRTUAL(CAboutDlg)
  23. protected:
  24. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  25. //}}AFX_VIRTUAL
  26. // Implementation
  27. protected:
  28. //{{AFX_MSG(CAboutDlg)
  29. //}}AFX_MSG
  30. DECLARE_MESSAGE_MAP()
  31. };
  32. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  33. {
  34. //{{AFX_DATA_INIT(CAboutDlg)
  35. //}}AFX_DATA_INIT
  36. }
  37. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  38. {
  39. CDialog::DoDataExchange(pDX);
  40. //{{AFX_DATA_MAP(CAboutDlg)
  41. //}}AFX_DATA_MAP
  42. }
  43. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  44. //{{AFX_MSG_MAP(CAboutDlg)
  45. // No message handlers
  46. //}}AFX_MSG_MAP
  47. END_MESSAGE_MAP()
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CDemoBlobInsertDlg dialog
  50. CDemoBlobInsertDlg::CDemoBlobInsertDlg(CWnd* pParent /*=NULL*/)
  51. : CDialog(CDemoBlobInsertDlg::IDD, pParent)
  52. {
  53. //{{AFX_DATA_INIT(CDemoBlobInsertDlg)
  54. // NOTE: the ClassWizard will add member initialization here
  55. //}}AFX_DATA_INIT
  56. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  57. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  58. }
  59. void CDemoBlobInsertDlg::DoDataExchange(CDataExchange* pDX)
  60. {
  61. CDialog::DoDataExchange(pDX);
  62. //{{AFX_DATA_MAP(CDemoBlobInsertDlg)
  63. // NOTE: the ClassWizard will add DDX and DDV calls here
  64. //}}AFX_DATA_MAP
  65. }
  66. BEGIN_MESSAGE_MAP(CDemoBlobInsertDlg, CDialog)
  67. //{{AFX_MSG_MAP(CDemoBlobInsertDlg)
  68. ON_WM_SYSCOMMAND()
  69. ON_WM_PAINT()
  70. ON_WM_QUERYDRAGICON()
  71. ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  72. ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
  73. //}}AFX_MSG_MAP
  74. END_MESSAGE_MAP()
  75. /////////////////////////////////////////////////////////////////////////////
  76. // CDemoBlobInsertDlg message handlers
  77. BOOL CDemoBlobInsertDlg::OnInitDialog()
  78. {
  79. CDialog::OnInitDialog();
  80. // Add "About..." menu item to system menu.
  81. // IDM_ABOUTBOX must be in the system command range.
  82. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  83. ASSERT(IDM_ABOUTBOX < 0xF000);
  84. CMenu* pSysMenu = GetSystemMenu(FALSE);
  85. if (pSysMenu != NULL)
  86. {
  87. CString strAboutMenu;
  88. strAboutMenu.LoadString(IDS_ABOUTBOX);
  89. if (!strAboutMenu.IsEmpty())
  90. {
  91. pSysMenu->AppendMenu(MF_SEPARATOR);
  92. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  93. }
  94. }
  95. // Set the icon for this dialog.  The framework does this automatically
  96. //  when the application's main window is not a dialog
  97. SetIcon(m_hIcon, TRUE); // Set big icon
  98. SetIcon(m_hIcon, FALSE); // Set small icon
  99. // TODO: Add extra initialization here
  100. ::CoInitialize(NULL);
  101. return TRUE;  // return TRUE  unless you set the focus to a control
  102. }
  103. void CDemoBlobInsertDlg::OnSysCommand(UINT nID, LPARAM lParam)
  104. {
  105. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  106. {
  107. CAboutDlg dlgAbout;
  108. dlgAbout.DoModal();
  109. }
  110. else
  111. {
  112. CDialog::OnSysCommand(nID, lParam);
  113. }
  114. }
  115. // If you add a minimize button to your dialog, you will need the code below
  116. //  to draw the icon.  For MFC applications using the document/view model,
  117. //  this is automatically done for you by the framework.
  118. void CDemoBlobInsertDlg::OnPaint() 
  119. {
  120. if (IsIconic())
  121. {
  122. CPaintDC dc(this); // device context for painting
  123. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  124. // Center icon in client rectangle
  125. int cxIcon = GetSystemMetrics(SM_CXICON);
  126. int cyIcon = GetSystemMetrics(SM_CYICON);
  127. CRect rect;
  128. GetClientRect(&rect);
  129. int x = (rect.Width() - cxIcon + 1) / 2;
  130. int y = (rect.Height() - cyIcon + 1) / 2;
  131. // Draw the icon
  132. dc.DrawIcon(x, y, m_hIcon);
  133. }
  134. else
  135. {
  136. CDialog::OnPaint();
  137. }
  138. }
  139. // The system calls this to obtain the cursor to display while the user drags
  140. //  the minimized window.
  141. HCURSOR CDemoBlobInsertDlg::OnQueryDragIcon()
  142. {
  143. return (HCURSOR) m_hIcon;
  144. }
  145. void CDemoBlobInsertDlg::OnButton1() 
  146. {
  147. // TODO: Add your control notification handler code here
  148. CString strPath="c:\source.bmp";//读取图片路径
  149. _ConnectionPtr conn;//连接数据库智能指针
  150. conn.CreateInstance(__uuidof(Connection));//生成连接实例
  151. //打开的数据源
  152. conn->Open(_bstr_t("Provider=OraOLEDB.Oracle.1;User Id=scott;Data Source=abc"),_bstr_t(""),_bstr_t("tiger"),-1);
  153. _RecordsetPtr rs;//对应查询结果的链表
  154. rs.CreateInstance(__uuidof(Recordset));//生成查询实例
  155. //查询字句及打开方式
  156. rs->Open(_variant_t("select * from MyIMGBooks"),
  157. conn.GetInterfacePtr(),
  158. adOpenDynamic,
  159. adLockPessimistic,
  160. adCmdText);
  161. rs->AddNew();
  162. //首先插入非BLOB字段;
  163. rs->Fields->GetItem(_variant_t("BookISNB"))->Value=_variant_t((long)123);
  164. rs->Fields->GetItem(_variant_t("BookName"))->Value=_variant_t("adsf");
  165. rs->Fields->GetItem(_variant_t("BookAuthor"))->Value=_variant_t("adsf");
  166. //插入BLOB字段;
  167. int iBufferSize=1024*200;//一次读取内存200K个字节
  168. BYTE* pBuffer=new BYTE[iBufferSize];//连续的字节内存空间
  169. SAFEARRAYBOUND bd[1];//设定安全数组边界
  170. bd[0].cElements=iBufferSize;//安全数组最大下标200K
  171. bd[0].lLbound=0;
  172. //创建字节、1维、指定边界的安全数组
  173. SAFEARRAY* pArray=SafeArrayCreate(VT_UI1,1,bd);
  174. //打开文件
  175.  CFile fsource;
  176.  CFileException ex;//异常保存
  177.  fsource.Open(strPath,CFile::modeRead,&ex);
  178.  int iReaded=0;
  179.  do 
  180.  {//获取连续字节内存的首地址
  181. BYTE* pTemp=pBuffer;
  182. //以指定大小200K读取图片到连续内存中,iReaded为读到数据的实际大小(最后一次不一定是200K)
  183. iReaded=fsource.Read(pBuffer,iBufferSize);
  184. //修改BUG;如果iReaded最后一次不是200K就要出现指定符合iReaded大小边界的安全数组
  185. if(iReaded!=iBufferSize)
  186. {
  187. SafeArrayDestroy(pArray);//清空之前的安全数组
  188. pArray=NULL;
  189. SAFEARRAYBOUND bd1[1];
  190. bd1[0].cElements=iReaded;//指定符合iReaded大小边界的安全数组
  191. bd1[0].lLbound=0;
  192. pArray=SafeArrayCreate(VT_UI1,1,bd1);
  193. }
  194. for(long i=0;i<iReaded;i++)
  195. {//把连续内存中的二进制数据复制到安全数组中去
  196. SafeArrayPutElement(pArray,&i,pTemp++);
  197. }
  198. VARIANT mydata;//智能型数据
  199. mydata.vt=VT_ARRAY|VT_UI1;//数组及字节类型
  200. mydata.parray=pArray;//获取(保存图片二进制数据)安全数组入口
  201. //根据属性名索引把mydata中的图片二进制数据上传(追加)到数据库
  202. rs->Fields->GetItem("BookImage")->AppendChunk(mydata);
  203.  } while (iReaded==iBufferSize);
  204. //更新数据库
  205.  rs->Update();
  206.  rs->Close();
  207.  conn->Close();
  208.  MessageBox("ok");
  209. }
  210. void CDemoBlobInsertDlg::OnButton2() 
  211. {
  212. // TODO: Add your control notification handler code here
  213. CString strPath="c:\source_bak.bmp";
  214. _ConnectionPtr conn;
  215. conn.CreateInstance(__uuidof(Connection));
  216. conn->Open(_bstr_t("Provider=OraOLEDB.Oracle.1;User Id=scott;Data Source=abc"),_bstr_t(""),_bstr_t("tiger"),-1);
  217. _RecordsetPtr rs;
  218. rs.CreateInstance(__uuidof(Recordset));
  219. rs->Open(_variant_t("select * from MyIMGBooks where Bookisnb=124"),
  220. conn.GetInterfacePtr(),
  221. adOpenDynamic,
  222. adLockPessimistic,
  223. adCmdText);
  224. CFile fTarget;
  225. CFileException ex;
  226. fTarget.Open(strPath,CFile::modeCreate|CFile::modeWrite,&ex);
  227. int iBufferSize=1024*100;
  228. _variant_t vt;
  229. int iReaded=0;
  230. if(!rs->adEOF)
  231. {
  232. long iAllSize=rs->GetFields()->GetItem(_bstr_t("BookImage"))->ActualSize;
  233. int iReadedAll=0;
  234.   do{
  235.   BYTE* pBuf=NULL;
  236.   vt=rs->GetFields()->GetItem(_bstr_t("BookImage"))->GetChunk(1024*100);
  237.   iReaded=vt.parray->rgsabound->cElements;
  238.   SafeArrayAccessData(vt.parray,(void **)&pBuf);
  239.   fTarget.Write(pBuf,iReaded);
  240.   fTarget.Flush();
  241.   SafeArrayUnaccessData(vt.parray);
  242. iReadedAll+=iReaded;
  243.   }while(iReadedAll<iAllSize);
  244. }
  245. rs->Close();
  246. conn->Close();
  247. fTarget.Close();
  248. MessageBox("ok!");
  249. }