MFC存储图片到SQL Server数据库

第一步:建立数据库表,比如:id char,pic image。

第二步:建立MFC单文档应用程序,再添加类CMyRecordset,基类选择CRecordset,导入数据库的刚建立的表。

第三步:在doc.h中加入 CMyRecordset m_pSet。

第四步:在view.h中加入CMyRecordset* pSet。

第五步:在view.cpp中的OnInitialUpdate()加入

            pSet=&GetDocument()->m_pSet;
            if(pSet->IsOpen())
            pSet->Close();
            pSet->Open();

第六步:存图片

void ****View::OnOpen()
{
 // TODO: Add your command handler code here
 CFileDialog filedlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"images Files (*.jpg)|*.jpg",this);
 CString path;
 if(filedlg.DoModal()==IDOK)
 {
  path=filedlg.GetPathName();
  CFile file;
  CFileStatus filestatus;
  file.Open(path,CFile::modeRead);
  file.GetStatus(filestatus);
  pSet->AddNew();
  pSet->m_pic.m_dwDataLength=filestatus.m_size;
  HGLOBAL hGlobal = GlobalAlloc(GPTR,filestatus.m_size);
  pSet->m_pic.m_hData = GlobalLock(hGlobal);
  file.ReadHuge(pSet->m_pic.m_hData,filestatus.m_size);
  pSet->m_id=_T("1");
  pSet->SetFieldDirty(NULL);
  pSet->SetFieldNull(NULL,FALSE);
  pSet->Update();
  GlobalUnlock(hGlobal);
 }
}

第七步:读取图片

void ****View::OnRead()
{
 // TODO: Add your command handler code here
 CFile file2;
 file2.Open("C://a.jpg",CFile::modeCreate|CFile::modeWrite);
 pSet->MoveFirst();//读第一条记录
 BYTE* content=new BYTE[pSet->m_pic.m_dwDataLength];
 memcpy(content,GlobalLock(pSet->m_pic.m_hData),pSet->m_pic.m_dwDataLength);
 file2.WriteHuge(content,pSet->m_pic.m_dwDataLength); 
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值